Jump to content

Sleep Function Swallows Echo Statement Before It


chaseman

Recommended Posts

I want the login script to echo out a statement, and then after 5 seconds (if the user hasn't clicked the link manually) I want it to redirect with the header redirect. I'm using the sleep function for the delay of the redirect. Here's the concerned portion of the login script:

 


		// check to see if the INPUT DATA matches the DATABASE COLUMNS!
		if ($nickname == $dbuser_name && sha1($password) == $dbuser_password) {

				// set a session after login
				$_SESSION['user_name'] = $dbuser_name;
				$_SESSION['user_id'] = $dbuser_id;

			echo "<center>You're logged in! 
			<a href='01.php'>Click here</a> to go to the main page.</center>";


			sleep(5); // seconds to wait
			header ('Location: 01.php');

				// password incorrect error message
				} else {
					echo "<center>Incorrect password!</center>";
			}

 

But the echo statement never gets printed out, any idea why it gets swallowed by the sleep function?

Link to comment
Share on other sites

You must have error reporting turned off, theoretically you should have received an error for changing the header after an echo.

 

The sleep command is brand new to me, and I'm not even sure I get the point of it in PHP... but I did find out how to invoke it in your crazy scenario - just send the browser a bunch of extra whitespace.

 

Add a few lines of this before or after your echo statement:

echo "                    ";

 

I'm not sure if your header redirect will even work with any echoed text present, but if that much of your code was already working for you, then adding the whitespace should solve the rest.

 

Personally I prefer inserting javascript redirect code, but I guess the server side sleep function might be a more universal solution for people with javascript disabled.

 

Link to comment
Share on other sites

I totally overlooked that echo and header don't mix, for some weird reason the header redirect would work without any problems without the sleep function, but even with the sleep function it would work, the echo statement would simply not get printed out.

 

Anyways I will simply look up a JavaScript redirect.

 

Thanks for your help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.