How can I refresh a page using PHP periodically? If I can not do it by PHP, what is the best recommended scenario?
- 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– Najzero2012-09-12 07:46:48 +00:00Commented Sep 12, 2012 at 7:46
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"); 4 Comments
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 Comments
header("Location: ".$_SERVER['PHP_SELF']);$_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.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 Comment
//Refresh by HTTP META => // Refresh by HTML META also this looks like a answer copied from above.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 Comments
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 :)header('Location: .'); seems to refresh the page in Chrome, Firefox, Edge, and Internet Explorer 11.
3 Comments
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 Comment
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 Comment
Adding this meta tag in PHP might help:
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">'; 2 Comments
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']); Comments
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 Comment
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 Comments
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.