Refresh a page using PHP - php

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

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

In PHP you can use:
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");
Or just use JavaScript's window.location.reload().

You sure can refresh a page periodically using PHP:
<?php
header("refresh: 3;");
?>
This will refresh the page every three seconds.

That is simply possible with header() in PHP:
header('Refresh: 1; url=index.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.

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

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

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

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.

Adding this meta tag in PHP might help:
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">';

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.

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.

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.

Related

How to add text in a php page

I have a PHP code to redirect on other domain, this one
<?php
header("Location: {$_GET['url']}");
echo 'You are redirecting to ... page';
sleep(5);
?>
And it works like when i add any external url at the end of http://my-domain.com/URL.php?url= it just redirects to external domain in this case let's take google:
http//my-domain.com/URL.php?url=http//www.google.com
and it just redirects directly on the page after 5 seconds load but i want to show a php page with some decoration like a loading icon and some text like "Please wait while you're being redirected to http://www.google.com"
Also i tried echo 'Please wait while you're being redirected'; But it just redirects directly to external domain without showing the message.
If you don't want javascript to do the timeout, you can add the refresh timer to the php header function:
header("Refresh: 5; url={$_GET['url']}");
Or go super old school and spit this out in the html:
echo '<meta http-equiv="refresh" content="5; url='. $_GET['url'] .'">';
Edit: I'd like to mention if you go old school, be sure to sanitize the url (dont just pass the GET variable out blindly like the above sample).
Try This
<?php
echo "Please wait...";
echo sprintf('
<script>
setTimeout(function(){
window.location.href = "http://stackoverflow.com";
},2000);
</script>
');

refresh div using (only using) php without page reload

Is there any way to refresh div by using php (no javascrip, jquery, ajax...). Without reloading my page.
I tried this but it reloads complete page.
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "5";
?>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
?>
It is impossible to refresh any div without reloading of whole page (without using of any form of JavaScript).
... and by the way, as somebody told me some time ago, questions where answer is YES or NO, are deprecated.

How to Redirect Page in PHP after a few seconds without meta http-equiv=REFRESH CONTENT=time

It seems that it is not advisable to use
<meta http-equiv=REFRESH CONTENT=3;url=url>
for redirects but instead use
header('Location: url')
However, I would like to show the user some message and allow them some time to read it before redirecting. Is there a way to do it without meta?
Try use "refresh" header:
header('Refresh: 3;url=page.php');
Also, you can look at this Question Refresh HTTP Header.
There is nothing wrong with using the meta refresh tag.
<meta http-equiv="refresh" content="5;URL='http://example.com/'" />
That tag says wait 5 seconds and redirect to example.com. This tag isn't a problem unless users are on IE6 and it still works, just breaks the history buttons.
Using JavaScript is an option, but make sure you include a link saying "If you are not automatically redirected, please click here". You should actually include that link either way.
php way to set header, will redirect you to test.php in 5 seconds:
header( "refresh:5;url=test.php" );
call before any actual output is sent.
And in javascript:
setTimeout(function () {
window.location.href= url; // the redirect goes here
},5000); // 5 seconds
Header tags are sent on page load, to the browser, so that it can quickly redirect the user to the desired page without bothering to render it or even load it into the history. As such, you can't call a redirect once the page has already loaded, as the headers have already been dealt with.
You can instead perform this with:
header( "refresh:5;url=wherever.php" );
Which basically sets the <meta> tag in the headers of the page itself, meaning you don't need to write the tag out.
By what you guys are saying, theoretically this should work then:
URL: http://www.example.com/ticketgen/index.php?success=1&redir=1
<?php
$myredir = ($_GET['redir']);
if ($myredir == 1)
{
header( "refresh:5;url=http://www.example.com/ticketgen/" );
}
?>
But it does nothing. I also have it at the VERY TOP of the page so it can send the headers.
it doesn't work in Firefox i just found out.
You can do it with a small piece of javascript:
<script type="text/javascript" language="JavaScript">location.href = 'otherpage.php';</script>
Of course, this will depend on the person having JavaScript enabled.
Obviously, to set the delay you can use something like setTimeout:
<script type="text/javascript" language="JavaScript">
setTimeout(function () {
location.href = 'stackoverflowhelp.php';
}, 5000);
</script>
I think really the best way is header("Refresh: 10;url=../index.php");
Like what i've done with my work.
https://codingislove.com/redirect-pages-php/
check out the above article, where they clearly explained about how to redirect the pages in PHP by setting time.
Redirecting code without time set:
header('location:URL ADDRESS');
Redirecting code with three seconds time set:
header('refresh:3; url=URL ADDRESS');

PHP refresh window? equivalent to F5 page reload?

Is there anything in PHP that is the equivalent of manually pressing the F5 page reload button? My php script is in a frame and isn't the parent script but it needs to refresh the entire page and not just it's frame.
Actually it is possible:
Header('Location: '.$_SERVER['PHP_SELF']);
Exit(); //optional
And it will reload the same page.
With PHP you just can handle server-side stuff. What you can do is print this in your iframe:
parent.window.location.reload();
If you have any text before a
header('Location: http://www.example.com/youformhere.php');
you'll have issues, because that must be sent before any other text is sent to the page.
Try using this code instead
<?php
$page = $_SERVER['PHP_SELF'];
echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
?>
Just remember, this code will create and infinite loop, so you'll probably need to make some conditional changes to it.
PHP cannot force the client to do anything. It cannot refresh the page, let alone refresh the parent of a frame.
EDIT: You can of course, make PHP write JavaScript, but this is not PHP doing, it's actually JavaScript, and it will fail if JavaScript is disabled.
<?php
echo '<script>parent.window.location.reload(true);</script>';
?>
<?php
echo "<script>window.opener.location.reload();</script>";
echo "<script>window.close();</script>";
?>
with php you can use two redirections.
It works same as refresh in some issues.
you can use a page redirect.php and post your last url to it by GET method (for example).
then in redirect.php you can change header to location you`ve sent to it by GET method.
like this:
your page:
<?php
header("location:redirec.php?ref=".$your_url);
?>
redirect.php:
<?php
$ref_url=$_GET["ref"];
header("location:redirec.php?ref=".$ref_url);
?>
that worked for me good.
Use JavaScript for this. You can do:
echo '
<script type="text/javascript">
parent.window.location.reload(true);
</script>
';
In PHP and it will refresh the parent's frame page.
guess you could echo the meta tag to do the refresh in regular intervals ... like
<meta http-equiv="refresh" content="600" url="your-url-here">
All you need to do to manually refresh a page is to provide a link pointing to the same page
Like this:
Refresh the selection
Adding following in the page header works for me:
<?php
if($i_wanna_reload_the_full_page_on_top == "yes")
{
$reloadneeded = "1";
}
else
{
$reloadneeded = "0";
}
if($reloadneeded > 0)
{
?>
<script type="text/javascript">
top.window.location='indexframes.php';
</script>
<?php
}else{}
?>

After DB login, redirect to another page

Hi I just posted this question PHP $_SESSION problem - I am able to log my user into the website/database just fine but instead of having it say "you have logged in, $username" - how can I say "you are logged in, $username" and in 3 seconds the page will redirect else where?
Thanks!
You could add a meta refresh tag to the header part of the page.
<meta http-equiv="refresh" content="3;url=http://domain.com/other.php">
I would still add a link to go there manually if the page doesn't automatically redirect; Refresh-headers can be disabled.
Using the meta HTML tag:
<meta http-equiv="refresh" content="3;url=http://domain/">
Using Javascript:
function redirect(url) {
document.location(url);
}
setTimeout('redirect("http://domain/")',3000)
You would need to echo out a meta tag to refresh the page after a delay, to look something like:
<meta http-equiv="refresh" content="3;url=http://site.com/">
Where the 3 is 3 seconds and site.com is the location.
Use sleep(3); followed by header("Location:YourLink");
The number in sleep is how many seconds to wait before continuing and the header command will redirect you wherever you'd like.
Just print out the message to the user before the sleep command and viola.

Categories