W D W D - 16 days ago 8
PHP Question

PHP IF / OR Not Working

I'm setting up a small magazine with several sections that all work from the same PHP page.

I'd like it to redirect people back to the home page if they are not on an official section. So for this my code is:

<?php
if (isset($_GET["section"])) {
$section = $_GET["section"];
} else {
header("Location: $site_url");
exit();
}

if ($section !== 'gallery' || $section !== 'magazine' || $section !== 'picks' || $section !== 'customs' || $section !== 'editor') {
header("Location: $site_url");
exit();
}
?>


The problem is, when I visit
localhost/results.php?section=gallery
, I'm redirected back to the home page, even though it is in the If statement. Anyone know why this is?

Answer

This will always be true:

$section !== 'gallery' || $section !== 'magazine' || ...

Because the same variable can never simultaneously be more than one value. I suspect you meant to use &&:

$section !== 'gallery' && $section !== 'magazine' && ...