-1

I'm trying to check for the existence of a variable coming over the URL. If the variables isn't present, I want to redirect the user to another page. If it is present, I want to continue executing the page.

I am unable to get the condition to evaluate false.


<?php 
$exist = isset($_GET["name"]); // isset() checks to see if $_get.name var exists
echo "exist outside: $exist";
var_dump($exist);
if ($exist != 1) // if var doesn't exist
{
        
    //header ("LOCATION: list.php");
    echo "exist inside: $exist";
    
    //echo '<meta http-equiv="refresh" content="0; URL=list.php">';
}   
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

0
if(isset($_GET["name"]) === false) {
    header("location: list.php");
    exit;
}
Douma
  • 2,682
  • 14
  • 23
  • It worked!! Thank you, Douma! So I it looks like I needed to use the comparison operator (===) and I didn't need to put "isset($_GET["name"]" into another variable. – Mom in MD Nov 09 '21 at 16:33
  • The only difference between this and your code snippet is that you were treating `$exist` as a number, but it's a boolean. – Bricky Nov 09 '21 at 17:54
  • actually, that doesn't make a difference, see this example where 1 equals true. https://onecompiler.com/php/3xgxgwajd. I think it went wrong on the headers already sent error when using header location. Anyway, the working example had made the code more readable and correct. – Douma Nov 09 '21 at 19:08