Home » Php » Redirect with Timer in PHP?

Redirect with Timer in PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

How can I make a redirect with PHP after say 10 seconds…

I have read alot about it, seems like it would be better with javascript. But PHP would save me alot of coding.

So how can I make a redirect with timer in PHP ?

Thanks

How to&Answers:

You can cause your PHP script to sleep for 10 seconds,

sleep(10);

but this will appear to the end-user as a non-responsive server. The best option is to use either a meta refresh,

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

or javascript.

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

Found in the comments from Daniel:

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

would be ideal for this situation, as it requires no Javascript or HTML.

Answer:

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

Place this PHP code inside header section of the page, otherwise, it wouldn’t work.

Answer:

You’ll want to do a client side redirect:

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

but if you feel like it has to be in PHP, you could do something like:

<?php

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

?>

Answer:

That is a bad idea to make PHP script sleeping. Actually it is a way to DoS your server easily 😉 PHP script in memory is consuming enough resources especially if it is working as CGI.

Answer:

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

Answer:

PHP is the probably wrong technology for this as it runs server-side and won’t provide useful feedback to the user during the delay.

This isn’t a lot of coding in Javascript. See this link for a very easy way to implement this on your page

Answer:

After the Web Page loads, PHP no longer runs. This means that you can’t do anything with PHP after the page loads unless you use something like AJAX(Javascript calling a PHP page) to transfer data to the page. This presents you with a few methods to achieve your desired 10 second wait on redirect.

First, you could tell your script to sleep() for 10 seconds. This however, as Johnathan mentioned, means that you would look like your page was really slow, only to have the user redirected.

sleep(10);

You could also simply drop in a META tag that tells the page to redirect itself after 10 seconds. This is the preferred method, as it doesn’t involved almost any other coding, as you simply drop in the META tag, and you don’t have to deal with javascript at all.

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

Then, you could also have Javascript issue a location.href=”bleh”; command after waiting for 10 seconds.

Answer:

Another method worth mentioning is sending a Location HTTP header with PHP’s header() function. So, if you want a robust solution that avoids javascript and meta tags, while keeping web application responsive, that might be creating an iframe (or a frame) and loading there a php script that contains the following:

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

Answer:

try this script and it will be work!

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

<body>
<?php echo $msg  ?>
</body>
</html>

Answer:

I managed to do it with a simple function on PHP

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

and you call from your PHP for waiting 2 seconds before redirect. Note that $waitmsg = 0.4 is used for defining the default value.

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

If you want to redirect after a form submit, 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
}

Answer:

Create a file, for example: “go.php”, with 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 this, 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>