rxgvhqkrywq6cxdjoar Posted December 12, 2009 Share Posted December 12, 2009 Hey folks, A quick question regarding headers and stuff... I'm getting the following error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/i_betcha_wanna_know/public_html/index.php:5) in /home/i_betcha_wanna_know/public_html/phpfunctions/sitewidefunctions.php on line 3 I read a lot of articles online about this error, and found a couple closed topics about it here on phpfreaks - but these topics do not suffice to answer my questions. My code is below... I've echoed out the first 10 lines or so rather than straight HTML because I read somewhere online that the first thing that should always appear in your .php pages is a <?php tag, and it should always end it with a ?>, with no white space above or below. I can recall getting other errors if this was not the case. After reading several other resources online, it appears that I must somehow call the session information before anything is sent to the browser. Do echoes count as something sent to the browser? This code works perfectly on my localhost WAMPserver, when I upload it to my web host however, I start getting all these retarded errors. How could I possibly extract the session information out of this log in page - it's embedded deep down in a few if statements, for loops, etc. session_start() is located at the very top of the sitewidefunctions.php function library I have created. The sitewidefunctions.php page is referenced in all pages that will either need sessions, or any other complex functions that specific page would need. For example, getmainlinksfade(), as shown below is located in this library. <?php echo " <html> <head> <title>TITLE</title> <link rel='stylesheet' type='text/css' href='CSS/global.css'/> <script type='text/javascript' src='JS/util-functions.js'></script> <script type='text/javascript' src='JS/clear-default-text.js'></script> </head> <body> <div id='wrap'> <div id='content'> <div class='maintitle'>TITLE</div>"; error_reporting(E_ALL & ~E_NOTICE); include "phpfunctions/sitewidefunctions.php"; include "phpfunctions/uniquevisitorcounter.php"; if(loggedin()) { header("Location: home.php"); exit(); } getmainlinksfade(); getsubtitlefade('Books'); getbooksfade('copyright'); $username = $_POST['username']; $password = $_POST['password']; $rememberme = $_POST['rememberme']; if($username&&$password) { $qry = mysql_query("SELECT * FROM users WHERE username='$username'"); if(mysql_num_rows($qry)==0) echo "<div id='index_errorlogin'> No user found <br/></div>"; else { while($row = mysql_fetch_assoc($qry)) { $db_password = $row['password']; if(md5($password)==$db_password) { $loginok=TRUE; mysql_query("UPDATE users SET isOnline=1 WHERE username='$username'"); } else { $loginok=FALSE; echo "<div id='index_errorlogin'>$username found! - Incorrect password <br/></div>"; break; } if($loginok==TRUE) { if($rememberme=='on') { setcookie("username", $username, time() + 7200); $_SESSION['username']=$username; } else $_SESSION['username']=$username; header("Location: home.php"); exit(); } else die(""); } } } else echo "<div id='index_errorlogin'>To view the entire library - Please log into the system below<br/></div>"; echo" <div id='index_form'> <form action='index.php' method='post'> <input type='text' name='username' value='username' class='cleardefault'><br/> <input type='password' name='password' class='cleardefault'><br/> <input type='checkbox' name='rememberme'><span id='index_rememberme'>Remember Me</span> <input type='submit' name='login' value='Login' id='index_login'> </form> </div> <br/><br/>"; getindexstats(); getfooter(); echo" </div> </div> </body> </html>"; ?> Can someone point me in the right direction? Alex from PHPAcademy uses this log-in technique - I watched his videos on Youtube and embedded his logic into my page. Again, this works perfectly on my WAMP! I know my question is kind of vague but any help is appreciated. Thanks and Cheers! rxgvhqkrywq6cxdjoar Quote Link to comment https://forums.phpfreaks.com/topic/184894-header-error/ Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 I suspect every thread you looked at had an appropriate answer for you... such as the 'Sticky' topic on the thread... http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Yes, echo's count as output, anything that outputs information to the client counts as output. You need to move any logic above the output of data. You are outputting <html> etc. which is not at all needed before the logic of redirecting. If your going to redirect them, what could you have possibly needed on the screen, since the user will never see it if redirected. Quote Link to comment https://forums.phpfreaks.com/topic/184894-header-error/#findComment-976048 Share on other sites More sharing options...
asmith Posted December 12, 2009 Share Posted December 12, 2009 As cags mentioned too, you have html data at the very beginning of your code getting echoed. So when you are redirecting your user, there are html on the top of the page. Try this: (and look how I edited your code) <?php error_reporting(E_ALL & ~E_NOTICE); include "phpfunctions/sitewidefunctions.php"; include "phpfunctions/uniquevisitorcounter.php"; if(loggedin()) { header("Location: home.php"); exit(); } getmainlinksfade(); getsubtitlefade('Books'); getbooksfade('copyright'); $username = $_POST['username']; $password = $_POST['password']; $rememberme = $_POST['rememberme']; if($username&&$password) { $qry = mysql_query("SELECT * FROM users WHERE username='$username'"); if(mysql_num_rows($qry)==0) echo "<div id='index_errorlogin'> No user found <br/></div>"; else { while($row = mysql_fetch_assoc($qry)) { $db_password = $row['password']; if(md5($password)==$db_password) { $loginok=TRUE; mysql_query("UPDATE users SET isOnline=1 WHERE username='$username'"); } else { $loginok=FALSE; echo "<div id='index_errorlogin'>$username found! - Incorrect password <br/></div>"; break; } if($loginok==TRUE) { if($rememberme=='on') { setcookie("username", $username, time() + 7200); $_SESSION['username']=$username; } else $_SESSION['username']=$username; header("Location: home.php"); exit(); } else die(""); } } } else $loginPlease = true; echo " <html> <head> <title>TITLE</title> <link rel='stylesheet' type='text/css' href='CSS/global.css'/> <script type='text/javascript' src='JS/util-functions.js'></script> <script type='text/javascript' src='JS/clear-default-text.js'></script> </head> <body> <div id='wrap'> <div id='content'> <div class='maintitle'>TITLE</div>"; if ($loginPlease) echo "<div id='index_errorlogin'>To view the entire library - Please log into the system below<br/></div>"; echo" <div id='index_form'> <form action='index.php' method='post'> <input type='text' name='username' value='username' class='cleardefault'><br/> <input type='password' name='password' class='cleardefault'><br/> <input type='checkbox' name='rememberme'><span id='index_rememberme'>Remember Me</span> <input type='submit' name='login' value='Login' id='index_login'> </form> </div> <br/><br/>"; getindexstats(); getfooter(); echo" </div> </div> </body> </html>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184894-header-error/#findComment-976054 Share on other sites More sharing options...
rxgvhqkrywq6cxdjoar Posted December 12, 2009 Author Share Posted December 12, 2009 Thanks so much cags and asmith, I have got my site working now thanks to both of you. asmith, i'm now using the index page you have supplied - and I have read over it and understood why we must do it this way. got it - all logic must be performed (especially headers) before any output is to be displayed. thanks all - this issue has been marked as resolved! Quote Link to comment https://forums.phpfreaks.com/topic/184894-header-error/#findComment-976071 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.