Scriptplayground

 Print Page | Close Window

Refresh Page Automatically

Redirect the page automatically using PHP. Very useful when Javascript may be disabled.

Refreshing the page has many uses. You could reload a news site or check for new content, maybe reload an ad where Javascript may be disabled on the browser, the possibilites are endless.

$urlRefresh = "thispage.php";
header("Refresh: 5; URL=\"" . $urlRefresh . "\""); // redirect in 5 seconds

That was simple enough, using "header" we refresh the page. In this example that will happen after 5 seconds. You can use any time imaginable, the value is in seconds. For instance if you had a news site that was refreshed every 5 minutes, you would use this

// 300 seconds is 5 minutes
60 * 5 = 300

One thing to keep in mind is this code MUST be placed before html code, otherwise you will get an error message like this. Warning: Cannot modify header information - headers already sent by (output started at /path/of/server/otherpage.php:65) in /path/of/server/thispage.php on line 20

One way to solve this problem is to use the output buffering on this page. At the top of the first document place this php.

ob_start(); // start output buffering

Then at the end of the last file, place this.

ob_end_flush(); // output the html and serve it to the browser

That's about it, now you can refresh a page when Javascript may be disabled.


Find this article at:
http://scriptplayground.com/tutorials/php/Refresh-Page-Automatically/