41

I am using following code for a refreshing page, it is not reloading on completion. The following code is not working sometime.

 $page = $_SERVER['PHP_SELF'];
 $sec = "10";
 header("Refresh: $sec; url=$page");
 echo "Watch the page reload itself in 10 second!";
nickb
  • 59,313
  • 13
  • 108
  • 143
Fou
  • 896
  • 2
  • 8
  • 19
  • 6
    Whoever downvoted this should post a comment stating their issue with the question. – HeatfanJohn Jul 16 '12 at 03:06
  • Should you edit it to remove "due to heavy load on page" if that really didn't have anything to do with the problem? – HeatfanJohn Jul 16 '12 at 03:18
  • @HeatfanJohn It's not bad to be the first to start something good. If the question is useful then why don't you up vote it yourself. Anyhow..I am up-voting it as it solved my problem. – Naeem Ul Wahhab Jan 05 '13 at 12:51

8 Answers8

71

Use a <meta> redirect instead of a header redirect, like so:

<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
    <head>
    <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
    </head>
    <body>
    <?php
        echo "Watch the page reload itself in 10 second!";
    ?>
    </body>
</html>
nickb
  • 59,313
  • 13
  • 108
  • 143
  • Aren't these two methods basically equivalent? http://en.wikipedia.org/wiki/URL_redirection#Refresh_Meta_tag_and_HTTP_refresh_header They both tell the browser to refresh the page in 10 seconds. If the web server is overwhelmed with requests, changing how to tell the browser how to refresh the page isn't really going to help, is it? – HeatfanJohn Jul 16 '12 at 03:05
  • 1
    @HeatfanJohn - For one, an HTTP header refresh is not in the HTTP standard. Second, this won't begin the timeout until after the HTML has been rendered, while the browser could receive the header information and act however it wants, since the behavior of a header redirect is not standardized. – nickb Jul 16 '12 at 03:12
  • Agreed, my main concern is that if his web server is overloaded, I don't believe this will help. Of course, if the real issue is that as you say the HTTP header refresh isn't universally honored while the meta refresh is, then your solution will help. – HeatfanJohn Jul 16 '12 at 03:15
  • @nickb - I've never before heard that header refresh presents any sort of compatibility problem. In fact [another SO answer](http://stackoverflow.com/questions/283752/refresh-http-header) outlined the Refresh header's history quite nicely. Compatibility shouldn't be an issue. And if that's the case ... what is? The question doesn't provide enough detail for us to tell what the actual problem is. – ghoti Jul 16 '12 at 03:27
  • @ghoti - I don't understand what you're asking. You linked an answer which is identical to the wikipedia link. The point is that the implementation of a header refresh is undefined, and whatever browser the OP is using could be handling it differently than expected. However, the `` refresh is standardized, so it's a sure thing, not to mention that the `` refresh solved the OP's problem. – nickb Jul 16 '12 at 03:35
  • @nickb - What I'm asking is ... what problem did your answer actually solve? If the only practical difference between the two methods is when the timeout starts, as you noted in a comment above, then the question needs clarification. WHEN does the refresh fail? While (like Set-Cookie) the Refresh header may not be included in the HTTP specification, it is supported by every browser, and the code in the OP's question *should* work reliably. – ghoti Jul 16 '12 at 10:54
32

you can use

<meta http-equiv="refresh" content="10" > 

just add it between the head tags

where 10 is the time your page will refresh itself

Adil Malik
  • 6,279
  • 7
  • 48
  • 77
Ram Aquino
  • 341
  • 3
  • 8
11

use this code ,it will automatically refresh in 5 seconds, you can change time in refresh

<?php $url1=$_SERVER['REQUEST_URI']; header("Refresh: 5; URL=$url1"); ?>

Junaid Akram
  • 111
  • 1
  • 2
5

Try out this as well. Your page will refresh every 10sec

<html>
 <head>

  <meta http-equiv="refresh" content="10; url="<?php echo $_SERVER['PHP_SELF']; ?>">
 </head>
 <body>

 </body>
</html>
Gados
  • 51
  • 1
  • 5
  • 1
    Hi Gideon afoh, Its not required to pass php_self in meta tag(i.e, url=). Without of this also it should refresh the current page on every 10 seconds. – Kaif Khan Apr 26 '17 at 19:46
2

Maybe use this code,

<meta http-equiv="refresh" content = "30" />

take it be easy

1

This works with Firefox Quantum 60+ and Chrome v72 (2019)

//set a header to instruct the browser to call the page every 30 sec
header("Refresh: 30;");

It does not seem to be NECESSARY to pass the page url as well as the refresh period in order to (re)call the same page. I haven't tried this with Safari/Opera or IE/Edge.

0

Simple step like this,

<!DOCTYPE html>
<html>
<head>
    <title>Autorefresh Browser using jquery</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            startRefresh();
        });
        function startRefresh() {
            setTimeout(startRefresh,100);
            $.get('text.html', function(data) {
                $('#viewHere').html(data);
            });
        }
    </script>

</head>
<body>
    <div id="viewHere"></div>
</body>
</html>

This video for complete tutorial https://youtu.be/Q907KyXcFHc

-2

<meta http-equiv="refresh" content="10" >

This can work. Try it..!! :-)

  • From Review: Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Oct 05 '18 at 06:43