开发者

How to freeze a PHP script for a few seconds?

开发者 https://www.devze.com 2023-01-15 11:44 出处:网络
I would like to make my PHP script freeze at a screen for at least 5 seconds before loading into the next script. I have tried \"Sleep()\" however that is not the function I am looking for as it only

I would like to make my PHP script freeze at a screen for at least 5 seconds before loading into the next script. I have tried "Sleep()" however that is not the function I am looking for as it only pause the script that is "going" to be loaded.

Here are my codes:

echo "Welcome ",$_SESSION['username']."<br>";
    echo "Click here to Logout :    ".'<br><a href="logout.php">Logout</a>';

    sleep(10);

    header("Location: ht开发者_如何学JAVAtp://192.168.11.32/phploginserver/test.php");

    echo '<script type="text/javascript">window.location="test.php"</script>';    
}

I would like the echo'to another page' to be delayed for at least 5 seconds before loaded so that my users can view their user name before being automatically redirected to the next page.


$time = new DateTime();
$newtime = $time->Modify("+5 seconds");



while($newtime > (new DateTime()))
    {
    // Do nothing
    }


You cannot freeze PHP script at a screen.
Just because there are no PHP scripts at screen. But merely HTML, rendered by browser.

Such a freezing considered bad practice and don't used nowadays.
Either show a message, if it's really important, or get user to the destination immediately (preferred).
You can use some AJAX-powered tips, as stackoverflow does.

so that my users can view their user name

Don't they know it already?
Can't they see it on the next page?
What if a user got disturbed and do not notice that message?


You can use an additional parameter with the php header to delay it. Like this:

header('Refresh: 10; url=http://192.168.11.32/phploginserver/test.php'); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号