JavaNoob
JavaNoob

Reputation: 3632

How to freeze a PHP script for a few seconds?

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: http://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.

Upvotes: 0

Views: 3970

Answers (3)

nathan hayfield
nathan hayfield

Reputation: 2685

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'); 

Upvotes: 0

oddi
oddi

Reputation: 387

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



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

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157919

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?

Upvotes: 0

Related Questions