Refresh a page using PHP

PhpRefresh

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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");

Solution 2 - Php

In PHP you can use:

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

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

Solution 3 - Php

You sure can refresh a page periodically using PHP:

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

This will refresh the page every three seconds.

Solution 4 - Php

That is simply possible with header() in PHP:

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

Solution 5 - Php

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.

Solution 6 - Php

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;"); 

Solution 7 - Php

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

Solution 8 - Php

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\">";

Solution 9 - Php

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.

Solution 10 - Php

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.

Solution 11 - Php

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.

Solution 12 - Php

Adding this meta tag in PHP might help:

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">';

Solution 13 - Php

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.

Solution 14 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAanView Question on Stackoverflow
Solution 1 - Phpuser1847051View Answer on Stackoverflow
Solution 2 - PhpAboQutieshView Answer on Stackoverflow
Solution 3 - Php131View Answer on Stackoverflow
Solution 4 - PhpKamleshView Answer on Stackoverflow
Solution 5 - PhpMathlightView Answer on Stackoverflow
Solution 6 - PhpThanosView Answer on Stackoverflow
Solution 7 - PhpBen GuestView Answer on Stackoverflow
Solution 8 - PhpPrakash PatilView Answer on Stackoverflow
Solution 9 - PhpPatriksView Answer on Stackoverflow
Solution 10 - PhpAnoopView Answer on Stackoverflow
Solution 11 - PhpJvdBergView Answer on Stackoverflow
Solution 12 - PhpSiddharth ShuklaView Answer on Stackoverflow
Solution 13 - Php0x45View Answer on Stackoverflow
Solution 14 - PhpEhsan MohebbiView Answer on Stackoverflow