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.
Related
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.
I constantly have to update main images on my site, the user will go to the site but the images won't be the updated versions unless they manually hit refresh. Even by me putting "please hit refresh to view updated images" the users ignore this and I have to e-mail them to hit the refresh button. I've tried having the initial index.html reload to the actual site using Javascript like this
The initial index.html:
document.location.href='index2.php?code=reload_page'
Then on the index2.php:
$the_code = $_GET['code'];
if($the_code == "reload_page")
{
$page = $_SERVER['PHP_SELF'];
$sec = "1";
header("Refresh: $sec; url=$page");
}
else
{
//load page regular
}
I tried it like this, but it didn't work, still has old images until you hit the refresh button. Any other ways of accomplishing this using PHP or javascript/jquery?
The problem with images not refreshing might be an issue with caching in web browser or on the server proxy etc. It is configuration issue and might be not dependant on you. Easy trick to bypass this is to add timestamp to img url. Every time you regenerate your content in index.php just add some query string to your image as this:
<?php
echo '<img src="my_image.png?ts='.time().'" />';
?>
it will trick your browser and proxies on the way that it is another image and prevent caching.
You can use timer to reload your images and get them via AJAX reqest form the other page:
setInterval(function(){
$.ajax({
url: 'index2.php'
}).done(function ( data ) {
$('#image-div').html(data);
});
}, 10000); // wait 10 seconds
Take a look at jQuery.ajax
Put the following in the head section of your page this will reload your content every 5 seconds.
<meta http-equiv="refresh" content="5">
If the browser still caches the images because the url hasn't changed then place a random query string on the end of the image url. You could use a timestamp.
<img src="image.jpg?<?php echo time(); ?>">
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{}
?>
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.
Is there a way to make page display few seconds in php and redirect to another page ?
The meta redirect is probably what you want, but you CAN do this in PHP too, like this:
<?php header("Refresh: 10;url=http://www.yourdestination.com/"); ?>
Where 10 is the number of seconds to wait.
EDIT Ok, I stand corrected. Corrected answer below.
You can either use PHP's header function as shown elsewhere on this page.
If you want to do a refresh after the page is rendered, you can do so with JavaScript or a Meta Refresh. For users that block meta refreshs and have JavaScript disabled it is good practise to provide a link that can be clicked manually to get to the new target.
Example:
<?php header("Refresh: 2;url=http://www.example.com/"); ?>
<html>
<head>
<title>Redirects</title>
<meta http-equiv="refresh" content="2; URL=http://example.com" />
<script type="text/javascript">
window.setTimeout(function() {
location.href = 'http://example.com';
}, 2000);
</script>
</head>
<body>
<p>Click here if you are not redirected automatically in 2 seconds<br />
Example.com.
</p>
</body>
</html>
Please also see the WCAG suggestions about Automatic Page Refreshes.
However, you're probably best off doing this in JavaScript
setTimeout(function()
{
window.location = "http://www.somedomain.com/somepage.php";
}, 5000); // 5 seconds
See #Gordon's answer a above for a more user-friendly and complete example, this is merely one method.
With META redirect you can:
<meta http-equiv="refresh" content="2;url=http://example.com/">
Where 2 is the delay in seconds.
Use the following code in PHP, but only after understanding this manual page fully (this is the main important part when using the following code):-
$redirectionTime = 5;
$newPageUrl = "wherever_page.php";
header( "Refresh: $redirectionTime; url=$newPageUrl" );
echo "You will now be redirected to a new page, after $redirectionTime seconds. Please be patient...";
exit();
The above code will redirect the user to the "wherever_page.php" page from the existing page after exactly 5 seconds. But you need to do another important thing.
You need to start the Output Buffer first, so that in case you output any HTML before calling the "header()" function, no warning or fatal error will be given. In order to do this, you need to call the following function at the very first line of your web page, whether you include anything or not:-
<?php
ob_start();
// Rest of the web page logic comes after this
The main advantage of the above sets of code is that even if the JavaScript is disabled for that browser, the redirection will still occur.
Hope it helps.