Timer redirect in PHP?

How can I do a redirect with PHP after 10 seconds ...

I read a lot about this, it seems like it would be better with javascript. But PHP will save me a lot of code.

So how can I do a timer redirect in PHP?

thanks

+6
php
source share
11 answers

You can make your PHP script sleep for 10 seconds,

sleep(10); 

but it will appear to the end user as a server that does not meet the requirements. The best option is to use meta-update,

 <meta http-equiv="refresh" content="10;url=http://google.com"> 

or javascript.

 setTimeout(function(){ window.location = "http://google.com"; }, 10000); 

Found in Daniel's comments:

 header('Refresh: 10; URL=http://yoursite.com/page.php'); 

would be ideal for this situation as it does not require Javascript or HTML.

+31
source share

header ('Refresh: 10; URL = http://yoursite.com/page.php');

Put this PHP code inside the header section of the page, otherwise it will not work.

+7
source share

Do you want to do client side redirection:

 <meta http-equiv="refresh" content="5;url=http://yourdomain.com"/> 

but if it seems to you that this should be in PHP, you can do something like:

 <?php // wait 5 seconds and redirect :) echo "<meta http-equiv=\"refresh\" content=\"5;url=http://yourdomain.com\"/>"; ?> 
+2
source share

It is a bad idea to sleep with a PHP script. Actually, this is a way to easily execute a DoS server;) A PHP script in memory consumes enough resources, especially if it works like CGI.

+2
source share

PHP is probably the wrong technology for this, since it works on the server side and will not provide useful feedback to the user during the delay.

This is not very much coding in Javascript. See this link for a very easy way to implement this on your page.

+1
source share
 <?php header( "refresh:10; url=https://example.com/index.php" ); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Redirect with countdown</title> <style type="text/css"> .myClass { font-family: verdana; font-size: 16px; font-weight: normal; color: black; line-height: 30px; } </style> </head> <body> <script type="text/javascript"> (function () { var timeLeft = 10, cinterval; var timeDec = function (){ timeLeft--; document.getElementById('countdown').innerHTML = timeLeft; if(timeLeft === 0){ clearInterval(cinterval); } }; cinterval = setInterval(timeDec, 1000); })(); </script> Redirecting in <span id="countdown">10</span> seconds. <br><br> <span class="myClass">Thank you! Your submission has been received!</span> </body> </html> 
+1
source share

After loading the web page, PHP no longer starts. This means that you cannot do anything with PHP after loading the page, unless you use something like AJAX (Javascript calling the PHP page) to transfer data to the page. This gives you several methods to achieve your desired 10 second redirect wait.

First, you can tell your script sleep () for 10 seconds. This, however, as Jonathan mentioned, means that you would look as if your page was very slow, just to let the user redirect.

 sleep(10); 

You can also simply add a META tag that tells the page to redirect itself in 10 seconds. This is the preferred method since it does not include almost any other encoding, since you just throw the META label and you don’t have to deal with javascript at all.

 <meta http-equiv="refresh" content="10;url=http://example.com"/> 

Then you may also have a problem with Javascript: location.href = "bleh"; after waiting for 10 seconds.

0
source share

Another way worth mentioning is to send an HTTP location header using PHP header () . Thus, if you need a reliable solution to avoid javascript and meta tags, while maintaining the responsiveness of the web application, this can create an iframe (or frame) and load a php script that contains the following:

 sleep(10); header("Location: http://my.redirect.location.com"); 
0
source share

try this script and it will work!

 <?php $msg ="This Is Just Checking"; header('Refresh: 5; URL=http://www.google.com.pk'); ?> <body> <?php echo $msg ?> </body> </html> 
0
source share

I managed to do this with a simple function in PHP

 function RedirectToURL($url, $waitmsg = 0.4) { header("Refresh:$waitmsg; URL= $url"); exit; } 

and you call from your PHP to wait 2 seconds before redirecting. Note that $ waitmsg = 0.4 is used to determine the default value.

 RedirectToURL("http://website.php", 2); 

If you want to redirect after submitting the form, you can try

 if(isset($_POST['submitted'])) { echo '<div style="text-align:center; margin-left:auto; margin-right:auto"><br><br>Sucess. Thanks for contribution.</div>'; RedirectToURL("http://thewebsiteilookforaftersubmit", 2); // I want to wait 2 seconds and not 0.4 as defined by default } 
0
source share

Create a file, for example: "go.php", with the code

 <?php $site = $_GET['iam']; sleep(5); Header ("Location: ".$site.""); exit(); ?> <a href="http://whatever.com/go.php?iam=http://google.com">Link</a> 

or like that, without "sleep"

 <?php $site = $_GET['iam']; Header('HTTP/1.1 301 Moved Permanently'); Header("Refresh: 10; URL=".$site.""); exit(); ?> <a href="http://whatever.com/go.php?iam=http://google.com">Link</a> 
0
source share

All Articles