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>
');
Related
I'm setting up a simple landing page on DreamHost. It won't let me put php code in the file index.html. So, when user submits an email address, I use $_POST to submit the email address to another page mail_auto.php.
After seeing a quick "email sent" message, I'd like the user to be directed back to the index.html page from mail_auto.php.
header() looks a bit complex and seems to interfere with the execution of the balance of mail_auto.php.
What's the best way to redirect the user?
To redirect user back to index.html, use the following:
header('Location: index.html');
exit;
Alternatively, if you want to display something like " Redirecting... " on screen, you can use the meta-refresh method , or JavaScript window.location method with setTimeout
The meta refresh method:
Add this to HTML <head>:
<meta http-equiv="refresh" content="2;url=index.html">
where 2 is number of seconds before the refresh is executed.
Using the header is typically what I'd do.
Have you thought about using JavaScript? It's not the best way, although it would work.
<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//-->
</script>
Just echo this javascript code end of the process.
<script>
window.location.href = 'http://www.yourwebsite.com';
</script>
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.
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');
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{}
?>