How can I refresh a page using PHP periodically? If I can not do it by PHP, what is the best recommended scenario?
14 Answers
You can do it with PHP:
header("Refresh:0");
It refreshes your current page, and if you need to redirect it to another page, use following:
header("Refresh:0; url=page2.php");

- 30,738
- 21
- 105
- 131
-
3*Refresh* is not an official header, but is supported by many browsers since Netscape Navigator. That means you will have to test to make sure your audience uses supporting browsers. – Patanjali Oct 23 '16 at 21:35
-
7Using *0* will continuously hammer your server, and/or your local cache, especially if you have images on the page. If you only need to update information on the screen, like stock prices, but not use that information in a form or from javascript, perhaps use an *iframe* tag pointing to a page with just the information being updated, and with a delay appropriate to how *current* the information *must* be. – Patanjali Oct 23 '16 at 21:37
-
2This causes ["Headers already sent” error](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) in my code... is there another way? – DBS Apr 28 '17 at 00:35
-
Check [here](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) for a solution; tldr, you can't output anything before using `header();` – adrian Aug 29 '17 at 04:11
In PHP you can use:
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");
Or just use JavaScript's window.location.reload()
.

- 30,738
- 21
- 105
- 131

- 1,696
- 2
- 9
- 14
-
2Yazana header('Location:') is used to redirect user to any url, you can not refresh page using it. – Patriks Sep 12 '12 at 07:47
-
2Pratik. You are wrong. you can indeed use header("refresh:") to refresh a page – 131 Aug 22 '13 at 19:47
-
7You can do both although I prefer no wait time with: `header("Location: ".$_SERVER['PHP_SELF']);` – John Magnolia Feb 24 '14 at 11:08
-
7A note about this solution: If your site is implementing some sort of nice routing or URL rewriting (i.e. almost any framework by default) you might find `$_SERVER['PHP_SELF']` gives you the true script path, and not the actual URL you see in your browser. In which case, try using `$_SERVER['REQUEST_URI']` instead. – BadHorsie May 25 '16 at 21:36
You sure can refresh a page periodically using PHP:
<?php
header("refresh: 3;");
?>
This will refresh the page every three seconds.

- 30,738
- 21
- 105
- 131

- 1,363
- 1
- 12
- 32
Besides all the PHP ways to refresh a page, the page will also be refreshed with the following HTML meta tag:
<meta http-equiv="refresh" content="5">
See Meta refresh - "automatically refresh the current web page or frame after a given time interval"
You can set the time within the content
value.

- 6,436
- 17
- 62
- 107
-
3this will refresh every 0 seconds - probably not what you're going for :) – warren Jul 25 '14 at 18:02
-
3yes I am commenting on this answer: there's always room for improvement. And you're right, you can set `content` equal to any value in seconds - my comment is still worth noting that a refresh of 0 seconds is probably not what you're going for :) – warren Jul 26 '14 at 00:26
-
2@Mathlight & whoever else complains about delayed comments. Just because you may have moved on, a future reader may have just experienced the issue, and your answer/comment, along with any delayed ones challenging it, may still be relevant to them. – Patanjali Oct 23 '16 at 21:11
-
1@Mathlight I'm not sure why people think that just because a post is old there can be no value in commenting on it. You should dispose of that thinking. – Mark Kramer Feb 18 '17 at 00:42
-
I've found two ways to refresh PHP content:
1. Using the HTML meta
tag:
echo("<meta http-equiv='refresh' content='1'>"); //Refresh by HTTP 'meta'
2. Using PHP refresh rate:
$delay = 0; // Where 0 is an example of a time delay. You can use 5 for 5 seconds, for example!
header("Refresh: $delay;");

- 30,738
- 21
- 105
- 131

- 341
- 2
- 4
-
2`//Refresh by HTTP META` => `// Refresh by HTML META` also this looks like a answer copied from above. – php_coder_3809625 Sep 04 '15 at 11:30
That is simply possible with header() in PHP:
header('Refresh: 1; url=index.php');

- 30,738
- 21
- 105
- 131

- 654
- 8
- 21
header('Location: .');
seems to refresh the page in Chrome, Firefox, Edge, and Internet Explorer 11.

- 30,738
- 21
- 105
- 131

- 1,488
- 2
- 17
- 29
-
As it should (once): http://php.net/manual/en/function.header.php: "The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set." – Sz. Dec 20 '17 at 23:59
-
But that's not what the OP asked for. I still upvoted it, because I realized that only afterwards, sorry. It's Xmas soon! :) – Sz. Dec 21 '17 at 00:02
-
The advantage to this to Refresh:0 is that this will load the page in one try but the other in two! – Steve Moretz Oct 24 '22 at 10:37
Echo the meta
tag like this:
URL is the one where the page should be redirected to after the refresh.
echo "<meta http-equiv=\"refresh\" content=\"0;URL=upload.php\">";

- 30,738
- 21
- 105
- 131

- 107
- 2
- 12
You can refresh using JavaScript. Rather than the complete page refresh, you can give the contents to be refreshed in a div. Then by using JavaScript you can refresh that particular div only, and it works faster than the complete page refresh.

- 30,738
- 21
- 105
- 131

- 130
- 2
- 10
-
3If you include the code, this answer might be a little more understandable. – see May 29 '16 at 05:20
Adding this meta tag in PHP might help:
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">';

- 30,738
- 21
- 105
- 131

- 1,063
- 15
- 14
-
3While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Mar 09 '17 at 22:57
-
One trick is to add a random number to the end of the URL. That way you don't have to rename the file every time. E.g.:
echo "<img src='temp.jpg?r=3892384947438'>"
The browser will not cache it as long as the random number is different, but the web server will ignore it.

- 30,738
- 21
- 105
- 131

- 779
- 3
- 7
- 26
-
2How does that refresh a page periodically? Are you responding to another answer? – Peter Mortensen Jul 19 '19 at 20:12
Add the following function to your project:
function redirect($filename) {
if (!headers_sent())
header('Location: '.$filename);
else {
echo '<script type="text/javascript">';
echo 'window.location.href = \''.$filename.'\';';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url=\''.$filename.'\'" />';
echo '</noscript>';
}
exit();
}
function call:
redirect($_SERVER['REQUEST_URI']);

- 97
- 2
- 9
PHP is server-side language, so you can not refresh the page with PHP, but JavaScript is the best option to refresh the page:
location.reload();
The visit Location reload() method.

- 30,738
- 21
- 105
- 131

- 1,012
- 1
- 9
- 29
-
1Hahahah ok then, as you wish. Check this out=> (http://wrongDomain.epl.fanxt.com/) he didn't specify that he will refresh the page 'after' it's loaded. If the the decision to refresh was made during the execution of the php code, then php certainly can refresh, and can choose after how many sec to refresh. – Mohammed Joraid Feb 15 '14 at 08:18
-
1@Ahmad, you can't consider as a feature of PHP, it is feature of HTML. So In sort you are doing by setting html response header. And that is why I said you can't do it with PHP. Another thing, you can't refresh page after loading/when needed. yes you can set it for some sec or etc as you answered. – Patriks Feb 17 '14 at 05:00
-
oh the technicalities. Think it is possible to use the header to refresh a specific image? That is if you stream the image through the php of coarse. – EZFrag Oct 03 '14 at 10:40
-
-
Ah yes, found it. At first I tried a normal image tag. No Luck. But then I replaced the image tag with the iframe tag, and voila. The image refreshes automatically. This is going to help a lot with some of my on page status images :) For those that are wondering, I'm serving the image via php along with the refresh header, setting the php file as the src of the iframe. – EZFrag Oct 06 '14 at 13:14
-
Again, don't you think 'refresh' header is feature by HTML? PHP just streams image (depending on user status or what ever) it does not refresh the page, HTML header (refresh) tells browser that it has to refresh content after x time. – Patriks Oct 06 '14 at 13:49
-
Technically, PHP cannot refresh the page, but by using the header *Refresh* or *meta* tag, as described elsewhere, PHP can *tell the browser* to display the current page *until* the delay has timed out, then refresh the page. – Patanjali Oct 23 '16 at 21:49
-
@Patriks Why do you keep using the word "technically" PHP does not refresh a page. Though it is server side language, but part of it's functionality includes the header tag which it uses to control the html header. What do you think happens with javascript when you use the window.location code, the javascript library uses the command to control the html header. So if you keep saying technically PHP doesn't refresh a page, you can as well say JavaScript does not and you are both wrong. It is the commandline in the tag specified that controls the header, so technically it does refresh a page. – optimalresource Sep 27 '18 at 14:12
You cannot do it in PHP. Once the page is loaded, PHP dies and is out of control.
You have a few options:
- Use JavaScript
- Use the refresh meta tag,
<meta http-equiv="refresh" content="5">
I think that the refresh meta tag is the easiest and most convenient.

- 30,738
- 21
- 105
- 131

- 21,777
- 8
- 38
- 55
-
66Wrong, you can refresh a page periodically using PHP, if you use PHP's header function like this: header("refresh: 3;"); – 131 Jul 31 '13 at 14:51
-
1You can refresh a page in PHP using header. Also, using META REFRESH is NOT ideal for SEO. – TheCarver Aug 31 '13 at 17:22
-
1I think this is wrong, always there are a way: I think if he wants to refresh page with php after some while can send an ajax request to a page that has refresh code of php. **but at all your answer is good** – Mohammad Kermani Jul 08 '14 at 10:25
-
2
-
Once the page is loaded, PHP dies and is out of control. All other methods are client side tricks, to connect again to the server and reload the page – JvdBerg Jan 16 '15 at 08:15
-
1This is completely wrong! In every possible way. It can be done by PHP, by headers (refresh) and by triggered redirects. It seems that all other answers are better. – Budimir Grom Feb 21 '15 at 10:07
-
1I have to chime in. The answer is correct, PHP dies after the page is served. The refresh header just creates the html meta tag for refresh. PHP does not refresh it, HTML does. – Rohit Gupta Aug 14 '22 at 04:38