dtyson2000 Posted June 28, 2012 Share Posted June 28, 2012 Ever have one of those days when you've looked and looked and tried and tried, yet everything has become totally jumbled and backwards and the obvious answer to a problem, which is probably staring you right in the face but you just don't see it? Yeah, that's me today/yesterday... I've been using this script (all one file... no separate form/processing files) for a year and everything has worked without a problem --- until I noticed that session_register() was deprecated and I made the changes necessary to no longer use it. I am getting a "Cannot modify header information - headers already sent" error, which I understand but I canNOT get past it since the addition of the isset($_SESSION condition. I need this script to check the date and depending on the last login date in the table either show a full list or a partial list. Then, once the user has clicked their name, insert their name and the time into a table, which is immediately followed by a "header: location" to avoid duplicate entries should someone click the reload button and to show who has not clicked their name yet today. The script isn't making it by that line since the addition of the isset($_SESSION condition. The insert into the database happens but I immediately get the "header already sent" error as a result and I no longer get the list of names that should be returned. Can someone please have a look at this and maybe offer some guidance explaining where I might put the "header: location" to get that reload to work without throwing the error? This is killing me. And thank you so much for taking the time to think about it! Here's a sanitized version of the code: <?php session_start(); if(isset($_SESSION['login']) && isset($_SESSION['password'])) { include ('header.php'); echo "<p>logged in as: ". $_SESSION['login']; checkdate(); include ('footer.php'); } else { header("location: login.php"); } function checkdate() { // check current date include ('config.php'); include ('opendb.php'); $today = date('Y-m-d'); $query = "SELECT * FROM datetable DESC limit 1"; $result = mysql_query($query) or die ("Couldn't execute query"); while ($row= mysql_fetch_array($result)) { $date = $row["date"]; switch(TRUE) { case ($date == $today) : if ($_SERVER['REQUEST_METHOD'] == "POST") { $userid = mysql_real_escape_string($_POST["userid"]); mysql_query ("INSERT INTO datetable (userid, date) VALUES ('$userid', DATE_ADD(NOW(), INTERVAL 1 HOUR))"); header("location: index.php"); } else { userlogin(); } break; case ($date != $today) : if ($_SERVER['REQUEST_METHOD'] == "POST") { $userid = mysql_real_escape_string($_POST["userid"]); mysql_query ("INSERT INTO datetable (userid, date) VALUES ('$userid', DATE_ADD(NOW(), INTERVAL 1 HOUR))"); header("location: index.php"); } else { userlist(); } break; } } } function userlist() { // form that shows all users } function userlogin() { // another form that shows only users who haven't logged in today } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264932-header-already-sent/ Share on other sites More sharing options...
memfiss Posted June 28, 2012 Share Posted June 28, 2012 u make output echo "<p>logged in as: ". $_SESSION['login']; then u are using header in ur function try to replace them session_start(); if(isset($_SESSION['login']) && isset($_SESSION['password'])) { checkdate(); include ('header.php'); echo "<p>logged in as: ". $_SESSION['login']; include ('footer.php'); } ... Quote Link to comment https://forums.phpfreaks.com/topic/264932-header-already-sent/#findComment-1357671 Share on other sites More sharing options...
dtyson2000 Posted June 28, 2012 Author Share Posted June 28, 2012 Ok... Thanks for the suggestion! I moved those items and I'm still getting the error. The error is pointing to the $today = date('Y-m-d') line and apparently considering it output but it needs to be there... Quote Link to comment https://forums.phpfreaks.com/topic/264932-header-already-sent/#findComment-1357696 Share on other sites More sharing options...
ManiacDan Posted June 28, 2012 Share Posted June 28, 2012 You cannot echo any data before making a header() call. You have an echo before your header. Also, you must exit() after any header() call. Quote Link to comment https://forums.phpfreaks.com/topic/264932-header-already-sent/#findComment-1357720 Share on other sites More sharing options...
redarrow Posted June 28, 2012 Share Posted June 28, 2012 not a solution but will work. <?php ob_start(); session_start(); //all code here ob_flush(); ob_end(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264932-header-already-sent/#findComment-1357736 Share on other sites More sharing options...
Maq Posted June 28, 2012 Share Posted June 28, 2012 Try reading this sticky: HEADER ERRORS - READ HERE BEFORE POSTING THEM Quote Link to comment https://forums.phpfreaks.com/topic/264932-header-already-sent/#findComment-1357738 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.