Need help with redirect

Looking for help in making code for an index.php file that redirects to another page if the date is past a certain date.

Not quite sure how to do this. It’s been years since I did php.

This is what I have, but it’s not working.

<? php
var date = new Date()
           .toISOString()
           .split('T')[0]; 
if(date >= "2018-09-24"){
   window.location.href = "http://google.com";
}
?>

This is the error:

Parse error : syntax error, unexpected ‘var’ (T_VAR) in /home4/dollar08/public_html/davemonroe/index.php on line 15

In PHP you can use the header function for redirection.

header('Location: http://www.example.com/');

It needs to be done before any other output statements (i.e. echo, printf);

Also, it’s a good idea to include a die() or exit statement after the header() function to make sure the redirect takes place.

It’s not about a good idea or the redirect taking place. You must have it or script will continue to run if you don’t.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service