-1

I'm having trouble getting an !isset test working properly. I tried a few different things but can't get it working as I expected.

Here is the scenario: if the get variable isn't set (i.e. someone visits the page directly and not to the page with the get variables in the url) then send them to another page.

This is what I tried:

if(!isset($_GET["e"])){$goToPage = 'Location: http://'.$_SERVER['SERVER_NAME'].'/';header($goToPage);}
if(!isset($_GET["k"])){$goToPage = 'Location: http://'.$_SERVER['SERVER_NAME'].'/';header($goToPage);}

I also tried this:

if(isset($_GET["e"])){}else{$goToPage = 'Location: http://'.$_SERVER['SERVER_NAME'].'/';header($goToPage);}
if(isset($_GET["k"])){}else{$goToPage = 'Location: http://'.$_SERVER['SERVER_NAME'].'/';header($goToPage);}

I know my variables are fine because if I echo the gets or assign them to variables, I get no problems:

$currentEmail = htmlspecialchars($_GET["e"]);
$currentKey = htmlspecialchars($_GET["k"]);

echo($_GET["e"]);
echo("</br>");
echo($_GET["k"]);

I also heard that by doing the check below, you can find out if someone came to the page by clicking a link. i.e. adding "a" instead of an actual get variable name. Does anyone know if that's true?

if(!isset($_GET["a"])){$goToPage = 'Location: http://'.$_SERVER['SERVER_NAME'].'/';header($goToPage);}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
as_bold_as_love
  • 216
  • 5
  • 11
  • I usually check whether `$_GET`'s or `$_POST`'s are empty rather than checking whether they're set. That would be for textfields/areas, selects but not radio's or checkboxes. – Edwin Lambregts Jun 11 '13 at 13:14
  • 1
    *"i know my variables are fine because if i echo the gets or assign them to variables i get no problems"* - sounds to me like they are set – billyonecan Jun 11 '13 at 13:15
  • 1
    are you attempting to do the redirects after you've already output content to the screen? header redirects need to go before everything else. – Dave Jun 11 '13 at 13:15
  • Define what it *does*! You're showing a bunch of code, but not what it does and how that differs from what you expect. – deceze Jun 11 '13 at 14:03

2 Answers2

1

The code you provided works absolutely fine and as expected on my system (Apache 2.2 on Debian).

You may need to check whether your Location: header is actually being sent. I'm guessing you're sending some output before your header() call, which will mean your header won't be sent at all - your header must be the absolute first output, which means you can't even have a blank line before your <?php tag. Depending on your error reporting settings, you may not see a warning when this happens. Take a look at PHP header redirect not working.

Community
  • 1
  • 1
ajd
  • 982
  • 1
  • 8
  • 19
  • the problem is not the header issue. i have another header redirect further down the page which works fine. i know seeting the header further down the page is not strictly legal but i do it all the time ad it works [i know i need to get out out of this dirty habit]. it is the !isset that is not working. – as_bold_as_love Jun 11 '13 at 13:25
  • It's not just a dirty habit - if there's any output before the `header()` call, it won't work at all under any circumstances. What's the context of this code? If you put `echo('My isset test works');` in the if statements, does it show up? – ajd Jun 11 '13 at 13:34
  • It's not whether the `header()` call is right at the top of the script - but whether there has been any **output** to the web browser before the `header()` call. It may be that the code around your header redirect here `echo`s something, but the code around your redirect later on doesn't `echo` anything. Do you have any `echo` statements or any other statements that will output text around your `header()` call? In any case - what happens if you put `echo('My isset test works');` in your `if` statement? – ajd Jun 11 '13 at 13:48
  • actually, im setting the header twice on the page. the first one [the isset one] is getting ignored if there is another header set after it. thats the problem. thanks for contribution. – as_bold_as_love Jun 11 '13 at 13:50
  • 1
    try adding a die(); after the first header set perhaps its output buffering and so only performing the secondary redirect rather than the primary. adding a die() straight after `header();die();` should break the script output the buffer and so do the first redirect othewise it'll just move onto the second redirect as usual – Dave Jun 11 '13 at 13:55
  • im running this header check before any output. its the first thing i do. im checking if there are get variables in the url [this is a password reset page]. i have another header reset further down the page but this is over writing the issest page. i just want the page to redirect instantly but i guess this isnt possible. – as_bold_as_love Jun 11 '13 at 14:01
  • change your query first to be an empty or strlen check rather than an isset as gets can be set even if null value ie: an empty post. changing your requirements will fix it "being ignored" and adding the die will make sure the first redirect gets processed first as sometimes where there's multiple redirects on the same page only the last one is obeyed – Dave Jun 11 '13 at 14:04
  • 1
    die(); after the first header redirect worked! excellent!! thanks! i understand it know - its running threw the whole page of code so the 2nd header is replacing the first one. by putting die() its just stopping in its tracks. thanks again ajd – as_bold_as_love Jun 11 '13 at 14:06
1

The script does not stop after you issue a Location header! If you issue another Location header later in the same script, it's going to overwrite the previous one! exit after you set the header to prevent the rest of the script from executing.

if (!isset($_GET["e"]) && !isset($_GET["k"])) {
    header('Location: http://' . $_SERVER['SERVER_NAME'] . '/');
    exit;  // <----- !!!
}
deceze
  • 510,633
  • 85
  • 743
  • 889