1

Spent ages going crazy over the simplest PHP If Statement; this time-based Redirect:

<?php

$time = date("Hi");

if ($time < "1400") {
header("Location: http://eurogamer.net");
}
else {
header("Location: http://ign.com");
}

?>

For AGES (I'm fairly new to PHP...) I couldn't figure out why the redirect worked, but wouldn't update to the new URL after 1400. Then I tested on another device and it DID work, but on both, it seems to store the redirect that people see when they're first directed to the PHP file, then send them there everytime.

As the plan here is to direct people to a different sign-up page depending on time of day, that's a problem if people revisit the page and get directed to an older sign up page that no longer works...

Is there a way to enforce the rules of this bit of code EVERY time they visit, or am I stuck here?

SOLVED: Switching the redirect from the PHP Header Function to Javascript seems to have solved the issue!

<?php

$time = date("Hi");

if ($time < "1400") {
    echo "<script type='text/javascript'>window.location.href = 'http://eurogamer.net';</script>";
die();
}
else {
echo "<script type='text/javascript'>window.location.href = 'http://ign.com';</script>";
die();
}

?>
SJS XI
  • 11
  • 3

2 Answers2

0

On using header(Location:) for redirection use die() or exit() after that.

 if ($time < "1400") {
    header("Location: http://eurogamer.net");
    die();
    }
    else {
    header("Location: http://ign.com");
    die();
    }

Refer

Php header location redirect not working

Community
  • 1
  • 1
Sanjuktha
  • 1,065
  • 3
  • 13
  • 26
0

I think you should send this as a 307 redirect (temporary) to ensure that browser cache did interfere:

<?php

  $time = date("Hi");

  if ($time < "1400") {
    header("HTTP/1.1 307 Temporary Redirect");
    header("Location: http://eurogamer.net");
  }
  else {
    header("HTTP/1.1 307 Temporary Redirect");
    header("Location: http://ign.com");
  }

?>

Also - to test it, try clearing all your browser cache before.

Esse
  • 3,278
  • 2
  • 21
  • 25
  • Tried this, but each device still just loads the original page used when first tested. On this PC, it actually does the same across browsers - Firefox is opening the same page that Chrome has initially accessed... – SJS XI Nov 16 '15 at 09:00