Noth
Noth

Reputation: 63

PHP - wait a couple of seconds before continuing?

I've set up a HTML form and I'm using PHP to verify that everything is good before displaying a message on the webpage. I then want to redirect the user back to the home page after a couple of seconds.

Problem is, I can't find a way to wait 5 seconds or so and then do the redirect. The sleep function simply waits 5 seconds and then displays the message/redirects at the same time. Anyone got any ideas?

Thanks!

<?php
    if (empty($_POST) === false) {
    echo 'Success! You will not be able to log in until an administrator approves your account.';
    sleep(5);
    $url = 'index.html';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';  
}
?>

Upvotes: 0

Views: 19020

Answers (6)

NinjaDestroyer
NinjaDestroyer

Reputation: 1

Here is something I worked up to redirect. Seems to work fine and thought I would share.

DelayTime(15); 

echo "<script>window.top.location.reload();</script>";

The DelayTime Function:

function DelayTime($secs){
      $now = time();
      while($now < ($now + $sec)){ 
        $now = time();
        if($now >= ($now + $sec)){ break; }
      }
      return true;
}

Upvotes: 0

Jon
Jon

Reputation: 437734

For a practical (works across the board AFAIK) but not standardized solution, use the Refresh HTTP header:

header('Refresh: 5; url=http://yoursite.com/index.html');

Otherwise, you can achieve the effect with a <meta> tag but without involving sleep():

// This goes inside <head>, so obviously *before* the "success" message!
echo '<META HTTP-EQUIV=Refresh CONTENT="5; URL='.$url.'">'; 

Upvotes: 6

Thom Wiggers
Thom Wiggers

Reputation: 7052

That is because the output is not sent to the browser sequentially. PHP sends out the output mostly in one go, after the script has terminated.

Instead use this meta-tag for waiting in html:

<meta http-equiv="Refresh" content="5; URL=/blah;">

Note that <meta> tags always should be put inside <head>.

Note that the browser can always choose to ignore redirects. And, although that is my personal opinion, redirects like this suck. Rather display a notice flash message at the top of the next pageload imho.

Upvotes: 1

chill0r
chill0r

Reputation: 1129

The way you do such redirections is javascript

Make a small javascript function like

function delayer() {
    window.location = "http://example.com"
}

and add it with

onLoad="setTimeout('delayer()', TIME_IN_MILLISECONDS_TO_WAIT)"

inside your html body tag

Upvotes: 0

mimipc
mimipc

Reputation: 1374

This should be done on the client side using javascript for example.

Upvotes: 2

Rhymond
Rhymond

Reputation: 110

<?php
    if (empty($_POST) === false) {
    echo 'Success! You will not be able to log in until an administrator approves your account.';
    sleep(5);
    $url = 'index.html';
    header("location: ".$url);  
}
?>

if it don't work need to use javascript

<script type="text/javascript">
setTimeout("window.location = 'index.html'", 2000);
</script>

Upvotes: -1

Related Questions