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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.