chaseman Posted March 29, 2011 Share Posted March 29, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232004-sleep-function-swallows-echo-statement-before-it/ Share on other sites More sharing options...
Nudd Posted March 29, 2011 Share Posted March 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232004-sleep-function-swallows-echo-statement-before-it/#findComment-1193515 Share on other sites More sharing options...
chaseman Posted March 29, 2011 Author Share Posted March 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232004-sleep-function-swallows-echo-statement-before-it/#findComment-1193637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.