kyles0623 Posted June 29, 2010 Share Posted June 29, 2010 I have this: function login_check(){ echo" WOOT!"; if(isset($_COOKIE['ID_my_site'])): echo "hello"; $username = $_COOKIE['ID_my_site']; echo" WOOT!"; $pass = $_COOKIE['KEY_my_site']; echo" WOOT!"; $sql = "SELECT * FROM users WHERE username = '$username' "; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ echo" here!"; if($pass != $row['password']): header("Location:login.php"); else: header("Location:login.php"); endif; } else: header("Location:login.php"); echo "hello"; endif; } If you notice in the else statement at the end there is a header redirect. It doesnt work. It does work though if i make it the first thing in the function. Also, the function echo's out the hello in the else statement at the end. This function is called before the head tag. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 29, 2010 Share Posted June 29, 2010 You cannot output ANYTHING (including a html doctype tag or any other character) before you use a php header() statement as that prevents the header() from working. If you were developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini, php will help you by reporting and displaying all the errors it detects. Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078927 Share on other sites More sharing options...
MadTechie Posted June 29, 2010 Share Posted June 29, 2010 It must be called before ANY output (including echo" WOOT!"; or echo "hello"; ) Any echo's will cause the header to failed! Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078928 Share on other sites More sharing options...
kyles0623 Posted June 29, 2010 Author Share Posted June 29, 2010 oh ok. So the echo's were stopping the header from working. Thankyou very much. Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078929 Share on other sites More sharing options...
plutomed Posted June 29, 2010 Share Posted June 29, 2010 Could you not put ob_start(); at the begining of the script and ob_end_flush(); at the end? Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078930 Share on other sites More sharing options...
MadTechie Posted June 29, 2010 Share Posted June 29, 2010 Could you not put ob_start(); at the begining of the script and ob_end_flush(); at the end? To could, or you could just do it correctly in the first place! Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078933 Share on other sites More sharing options...
plutomed Posted June 29, 2010 Share Posted June 29, 2010 Could you not put ob_start(); at the begining of the script and ob_end_flush(); at the end? To could, or you could just do it correctly in the first place! Yer ok ok lol. I just thought aswell. If you're gonna redirect to another page. Why echo on that page? Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078934 Share on other sites More sharing options...
kyles0623 Posted June 29, 2010 Author Share Posted June 29, 2010 I was echoing for testing. lol. Quote Link to comment https://forums.phpfreaks.com/topic/206223-header-not-redirecting/#findComment-1078937 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.