Redirect using META tags / HTML.
One of the simplest methods involves the usage of a META tag, which is placed inside the HEAD element of your HTML:
<!-- A meta tag that redirects after 5 seconds to one of my PHP tutorials--> <meta http-equiv="refresh" content="5;url=https://thisinterestsme.com/detecting-ajax-requests-with-php/">
A few pointers on this method:
- The “http-equiv” tag / property basically defines what type of header we are setting. In this case, we are giving it a value of “refresh”.
- Inside the content property, we declare that we want to redirect to the URL above after 5 seconds. If you want to redirect after 2 seconds or 10 seconds, then simply replace the number 5.
- Note that headers like this can be completely ignored by the client.
- The content of the page WILL display before the refresh takes place.
Redirect using PHP.
There are two ways to redirect using PHP. The method that you choose should depend on whether you want the user to see the content of the page before the redirect takes place.
If you are OK with the user seeing the content of the page:
<?php //Set Refresh header using PHP. header( "refresh:5;url=https://thisinterestsme.com/php-forcing-https-over-http/" ); //Print out some content for example purposes. echo 'This is the content.';
If you run the PHP snippet above, you’ll see that the message is displayed for five seconds. Then, the redirect occurs.
If you do NOT want the user to see the content (for whatever reason), then you can do the following:
<?php //Sleep for five seconds. sleep(5); //Redirect using the Location header. header('Location: https://thisinterestsme.com/php-forcing-https-over-http/'); //exit to prevent the rest of the script from executing exit;
Personally speaking, I can not think of a single sane reason for using the PHP code above.