Redirect page after five seconds using PHP, JavaScript or META tags.

This is a guide on how to redirect a web page after five seconds (or any other specified period of time). In this tutorial, I will show you how to do a “delayed redirect” using PHP, JavaScript and META tags.

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 few pointers on this method:

  1. 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”.
  2. 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.
  3. Note that headers like this can be completely ignored by the client.
  4. 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:

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:

Personally speaking, I can not think of a single sane reason for using the PHP code above.

Redirecting with JavaScript.

You can also use JavaScript to redirect the page after a certain period of time:

In the JavaScript code above, we use the setTimeout to execute a function after a set period of time. Basically, after the specified time period has elapsed, the function that was passed to setTimeout is executed. In this case, our function simply redirects the user to a different page. Note that setTimeout’s second parameter should be in milliseconds. In our example, we set it to 5000 milliseconds, which equates to 5 seconds.

Comments

comments