204

How can I refresh a page using PHP periodically? If I can not do it by PHP, what is the best recommended scenario?

1
  • are we talking about sync (you encounter a situation where you want to refresh during your php script is parsed) refrehs ( meta refresh would work fine) or about async continuous refresh of a page (more like javascript/ajax)?
    – Najzero
    Sep 12, 2012 at 7:46

14 Answers 14

374

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");
4
  • 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, 2016 at 21:35
  • 7
    Using 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, 2016 at 21:37
  • 2
    This causes "Headers already sent” error in my code... is there another way?
    – DBS
    Apr 28, 2017 at 0:35
  • Check here for a solution; tldr, you can't output anything before using header();
    – adrian
    Aug 29, 2017 at 4:11
124

In PHP you can use:

$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");

Or just use JavaScript's window.location.reload().

4
  • 2
    Yazana header('Location:') is used to redirect user to any url, you can not refresh page using it.
    – Patriks
    Sep 12, 2012 at 7:47
  • 2
    Pratik. You are wrong. you can indeed use header("refresh:") to refresh a page
    – 131
    Aug 22, 2013 at 19:47
  • 7
    You can do both although I prefer no wait time with: header("Location: ".$_SERVER['PHP_SELF']); Feb 24, 2014 at 11:08
  • 8
    A 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, 2016 at 21:36
21

You sure can refresh a page periodically using PHP:

<?php
    header("refresh: 3;");
?>

This will refresh the page every three seconds.

0
19

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;"); 
1
  • 2
    //Refresh by HTTP META => // Refresh by HTML META also this looks like a answer copied from above. Sep 4, 2015 at 11:30
19

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.

5
  • 3
    this will refresh every 0 seconds - probably not what you're going for :)
    – warren
    Jul 25, 2014 at 18:02
  • 3
    yes 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, 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.
    – Patanjali
    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
  • What do you mean by "Build in the if statement from"? Jul 19, 2019 at 19:52
18

That is simply possible with header() in PHP:

header('Refresh: 1; url=index.php');
6

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

3
  • 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
3

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\">";
1
  • But the question is not about redirect. It is about refresh. Jul 19, 2019 at 20:03
1

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.

1
  • 3
    If you include the code, this answer might be a little more understandable.
    – see
    May 29, 2016 at 5:20
0

Adding this meta tag in PHP might help:

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">';
2
  • 3
    While 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.
    – DimaSan
    Mar 9, 2017 at 22:57
  • But the question is not about redirect. It is about refresh. Jul 19, 2019 at 20:08
0

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.

1
  • 2
    How does that refresh a page periodically? Are you responding to another answer? Jul 19, 2019 at 20:12
0

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']);
-1

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.

8
  • 1
    Hahahah 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.
    – Patriks
    Feb 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.
    – EZFrag
    Oct 3, 2014 at 10:40
  • Really @EZFrag, I don't know, can you tell me how is it possible?
    – Patriks
    Oct 6, 2014 at 4:22
  • 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 6, 2014 at 13:14
-2

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.

7
  • 66
    Wrong, you can refresh a page periodically using PHP, if you use PHP's header function like this: header("refresh: 3;");
    – 131
    Jul 31, 2013 at 14:51
  • 1
    You can refresh a page in PHP using header. Also, using META REFRESH is NOT ideal for SEO.
    – TheCarver
    Aug 31, 2013 at 17:22
  • 1
    I 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
    Wrong ! Please fix your answer... Indeed this can be done with PHP
    – M_R_K
    Jan 16, 2015 at 5:39
  • 1
    This 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.