Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Refresh Page Automatically

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.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Refresh Page Automatically

 Dave   Fri Jul 20, 2007 5:11 pm  
Hi, just want to thank you for the tip.
Life saver!
 praveen  Mon Aug 13, 2007 10:02 am  
will have to try
 padmaja  Mon Feb 18, 2008 7:38 am  
i want to refresh a particular part in a page not the entire page
i want that code in php to do that functionality
 mkeefe  Mon Feb 18, 2008 8:06 am  
@padmaja, That is not possible with ONLY PHP, reason being, PHP runs on the server and not on the users machine. You could use AJAX to reload a portion of the page, or some similar technique.
 Soumya S Nambiar  Thu May 14, 2009 5:03 am  
actually i am doing an image editting program.I am doing the editing in a seperate PHP page. And after updating the contents in the database in am redirecting the page to the actual page. But i have to click the refresh button of IE to show the updated image. Can u help me please?
 mkeefe  Mon May 25, 2009 4:17 pm  
@Soumya - It sounds like you are outputting your page prior to the upload being completed. Make sure your page output is the last step in your code.
 Azhar  Fri Aug 7, 2009 1:29 pm  
Thank You very much.
 sarimah Muhamad Yunus  Wed Nov 11, 2009 2:31 am  
i like t refresh my page
 sarimah Muhamad Yunus  Wed Nov 11, 2009 2:32 am  
i like to try firt
 Shafi  Sun Jan 24, 2010 9:24 pm  
Hey! that's great, thankx a lot
 radih  Thu Mar 18, 2010 6:10 am  
thx..it's work...m/
 Bhupendra Tiwari  Sat Mar 5, 2011 3:41 am  
I want to only refresh my ajax part not refresh all part of my site so what i do
 mkeefe  Mon Apr 4, 2011 5:31 pm  
@Bhupendra - That is beyond the scope of this example, but you'd want to use Javascript to only reload the portion you're interested in.
 service  Sat Sep 10, 2011 11:01 am  
thank u bro, this work
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles