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");
-
3Refresh 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. Oct 23, 2016 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. Oct 23, 2016 at 21:37
-
2This causes "Headers already sent” error in my code... is there another way?– DBSApr 28, 2017 at 0:35
-
In PHP you can use:
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");
Or just use JavaScript's window.location.reload()
.
-
2Yazana header('Location:') is used to redirect user to any url, you can not refresh page using it.– PatriksSep 12, 2012 at 7:47
-
2Pratik. You are wrong. you can indeed use header("refresh:") to refresh a page– 131Aug 22, 2013 at 19:47
-
7You can do both although I prefer no wait time with:
header("Location: ".$_SERVER['PHP_SELF']);
Feb 24, 2014 at 11:08 -
8A 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. May 25, 2016 at 21:36
You sure can refresh a page periodically using PHP:
<?php
header("refresh: 3;");
?>
This will refresh the page every three seconds.
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;");
-
2
//Refresh by HTTP META
=>// Refresh by HTML META
also this looks like a answer copied from above. Sep 4, 2015 at 11:30
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.
-
3this will refresh every 0 seconds - probably not what you're going for :)– warrenJul 25, 2014 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 :)– warrenJul 26, 2014 at 0: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. Oct 23, 2016 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. Feb 18, 2017 at 0:42
-
header('Location: .');
seems to refresh the page in Chrome, Firefox, Edge, and Internet Explorer 11.
-
As it should (once): 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, 2017 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, 2017 at 0:02
-
The advantage to this to Refresh:0 is that this will load the page in one try but the other in two! Oct 24, 2022 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\">";
-
But the question is not about redirect. It is about refresh. Jul 19, 2019 at 20:03
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.
-
3If you include the code, this answer might be a little more understandable.– seeMay 29, 2016 at 5:20
Adding this meta tag in PHP might help:
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">';
-
3While this code snippet may solve the question, including an explanation 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.– DimaSanMar 9, 2017 at 22:57
-
But the question is not about redirect. It is about refresh. Jul 19, 2019 at 20:08
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.
-
2How does that refresh a page periodically? Are you responding to another answer? Jul 19, 2019 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']);
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.
-
1Hahahah ok then, as you wish. Check this out=> (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. Feb 15, 2014 at 8: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.– PatriksFeb 17, 2014 at 5: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.– EZFragOct 3, 2014 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.– EZFragOct 6, 2014 at 13:14
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.
-
66Wrong, you can refresh a page periodically using PHP, if you use PHP's header function like this: header("refresh: 3;");– 131Jul 31, 2013 at 14:51
-
1You can refresh a page in PHP using header. Also, using META REFRESH is NOT ideal for SEO. Aug 31, 2013 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 Jul 8, 2014 at 10:25
-
2
-
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. Feb 21, 2015 at 10:07