PHP refresh window? equivalent to F5 page reload? - php

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{}
?>

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

How to combine this PHP code with HTML and Javascript?

for example, this is my PHP code:
<?php
if($country_code == 'US')
{
header('Location: http://www.test.com');
}
else
{
header('Location: http://www.test.com/');
}
?>
I'm trying to use a Javascript code for tracking, it has to be above the </body> tag.
I have tried different ways of combining the PHP code with HTML, I have tried placing the HTML separately below the PHP also, one example:
<html>
<head></head>
<body>
<?php
?>
<script></script>
</body>
</html>
The furthest I got was, it tracked the click but it didn't redirect giving me this error:
`Warning: Cannot modify header information - headers already sent by`
Will appreciate any suggestions and help, thank you!
Put the php redirect code at the begining of your document before anything is outputted. Check for spaces after the ?> tag and before the <?php tag because these will be printed out and the response header will be sent therefor you will not be able to modify the header to redirect.
You have to try something like the following to track:
<?php
if($country_code == 'US')
{
header('Location: http://www.test.com/?us=yes');
}
else
{
header('Location: http://www.test.com/?us=no');
}
?>
And then in your index page check for the value of the us parameter. Also you should Notice that there is no any output should be printed before the header function to void the warning :
Warning: Cannot modify header information - headers already sent by
The trouble is that PHP run before JavaScript. So you need to geet the PHP variable inside a JavaScript.
<?php
// your normal code here, like connection to DB
?>
<script>var test = <?php> echo $thatVariable; <?> </script>
You may not send an output to the client before a header() tag of php.
So you can generate a redirect page which gets the country information via js and send it to the php (using e.g form submit). after that you can redirect to the accoording page via php header()
<?php
if($country_code === 'US'){
echo "<script> window.location.href='http://www.test.com1'</script>";
}
else{
echo "<script> window.location.href='http://www.test.com2/'</script>";
}
?>

PHP header redriction to new page

I would like to click on a hyperlink which will go to page 2, but I also want to open a new blank page.
I tried:
header "Location: http://bing.com";
header ("Location: http://bing.com", false);
header ("Location: http://bing.com", true, 301);
I'd like to use only php to open the new blank page.
You cannot do this from the header redirection. You need target="_blank" attribute added to your <A> in HTML. Or do the trick where your redirection redirects to the page that would use JavaScript to do that for you (expect it to be blocked by the browsers anti-popup feature though)
You cannot open a new window from doing a header redirection. You must use Javascript.
If you need to do it with PHP for some reason, you could print the Javascript like this:
<?php
echo '<script>window.open("http://www.google.com/");</script>';
?>
This is not possible with PHP. If you want to open 2 pages at once (replace the current, and open a second tab/window) you must do this with javascript.
One of the perfect solution to it which I found here only and you can do one thing is that which page you want to open along with other one put the code below in a PHP page the one needs to be opened and this code will pop up for the link provided in place of default link.
<?php
echo "<script type=\"text/javascript\"> window.open('http://reliable4you.com/', '_blank') </script>";
?>
This is my code for new tab and its working perfectly.
Just only care about your file location.
CODE:
<?php
if(isset($_POST['button']))
{
echo '<script language="javascript">';
echo 'window.open( "/Projectfolder/File.php" )';
echo '</script>';
}
?>

Refresh a page using 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.

php script to stay for few second in some page and redirect

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.

Categories