Thursday, January 5, 2012

Automatic Page Refresh using php

In this tutorial we'll see how to refresh a page after a preset amount of time (usually seconds or minutes).
Functions like this are used in big portals to automatically refresh the home page (or subpages) in order
to give always updated news.
<?php
header("Refresh: 120;");
?>
In this exmaple the current page is automatically reloaded after 2 minutes, note that time is expressed in seconds so to have 2 minutes between 2 refreshed you need to type 120
You can use this php line also to redirect (with the preselected delay) to another page of your site, or external to your site.

This will send the visitor to the forums (assuming you have the forums running under the folder "forums" as ours) after 6 seconds.
<?php
header("Refresh: 6; url=/forums/");
?>
This will send the visitor to www.php.net after 10 seconds.
<?php
header("Refresh: 10; url=http://www.php.net");
?>
Remember that using the PHP header function you always have to put it at the very beginning of your page, just after the <html> tag. Any other header sent before our header function will make it cause an error.

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts