doubledee Posted March 9, 2012 Share Posted March 9, 2012 I added some code last night that updates the "last_activity" field in the "member" table whenever the "body_header.inc.php" script is called, which means that pretty much whenever the User navigates to a new page or submits a form this field is updated. (I use this to kep my "User Online Status" up-to-date.) Everything was working fine until I suddenly started getting a "Cannot modify header" error before bed. Here is ONE sequence causing this error... - I am logged out - I am on http://local.debbie/index.php - I click on the "Log In" link - I am taken to http://local.debbie/members/log_in.php - I log in - I get this error... Warning: Cannot modify header information - headers already sent by (output started at /Users/user1/Documents/DEV/++htdocs/05_Debbie/index.php:22) in /Users/user1/Documents/DEV/++htdocs/05_Debbie/components/body_header.inc.php on line 48 Here is part of my main index.php script... <?php //Build Date: 2012-03-08 // Initialize a session. session_start(); // Access Constants. require_once('config/config.inc.php'); // Set current Script Name. $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME']; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- ################## DEBBIE ##################### --> <!-- HTML Metadata --> <title>Double Dee, Inc.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- Page Stylesheets --> <link type="text/css" rel="stylesheet" href="css/_main.css" /> <link type="text/css" rel="stylesheet" href="css/_layout.css" /> <link type="text/css" rel="stylesheet" href="css/top_menu.css" /> <link type="text/css" rel="stylesheet" href="css/components.css" /> </head> <body> <div id="pageWrapper" class="clearfix"> <div id="pageInner"> <!-- BODY HEADER --> <?php require_once('components/body_header.inc.php'); ?> <!-- LEFT COLUMN --> <div id="pageLeftCol"> Here is a snippet from my body_header.inc.php script... <?php //Build Date: 2012-03-08 // ************************ // Update Last Activity. * // ************************ if ((isset($_SESSION['loggedIn'])) && ($_SESSION['loggedIn'] == TRUE)){ // Initialize Session. // session_start(); // Access Constants. // require_once('../config/config.inc.php'); // Initialize variables. $loggedIn = TRUE; $memberID = (isset($_SESSION['memberID']) ? $_SESSION['memberID'] : ''); // ************************ // Update Member Record. * // ************************ // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); // Build query. $q1 = "UPDATE member SET logged_in=?, last_activity=now() WHERE id=? LIMIT 1"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variables to query. mysqli_stmt_bind_param($stmt1, 'si', $loggedIn, $memberID); // Execute query. mysqli_stmt_execute($stmt1); // Verify Update. if (mysqli_stmt_affected_rows($stmt1)!==1){ // Update Failed. $_SESSION['resultsCode'] = 'MEMBER_UPDATE_FAILED_2126'; // Redirect to Display Outcome. header("Location: " . BASE_URL . "members/results.php"); // End script. exit(); }//End of UPDATE MEMBER RECORD // Close prepared statement. mysqli_stmt_close($stmt1); // Close the connection. mysqli_close($dbc); /* */ }//End of UPDATE LAST ACTIVITY /* // Determine Current Script. $page = basename($_SERVER['REQUEST_URI']); if ($page == '') { $page = "index.php"; } */ // Determine Script Name. $scriptName = $_SERVER['SCRIPT_NAME']; ?> <!-- PAGE HEADER --> <div id="pageHeader"> <!-- COMPANY BRANDING --> <h1 id="companyLogo"> <!-- Display Logo if "Images On" --> <a href="/index.php"> <!-- Image Replacement Technique --> <span></span> </a> <!-- Display Text if "Images Off" --> DoubleDee, Inc: Tips on starting a Small-Business </h1> <!-- WELCOME MESSAGE --> <?php $firstName = (isset($_SESSION['memberFirstName']) ? $_SESSION['memberFirstName'] : ''); I was mindful of extra white space possibly causing the issue, but I don't see where it is?! The "Update Last Activity" code was added to my Header last night and is likely the culprit... Any ideas what is wrong?? Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
batwimp Posted March 9, 2012 Share Posted March 9, 2012 There is a sticky on this board titled HEADER ERRORS - READ HERE BEFORE POSTING THEM. Go into that post and read it. It will give you valuable information about this. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 At what stage are you including body_header.inc.php? I would hazard a guess as to it being before your session_start() statement thus giving you the error. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 Any ideas what is wrong?? Just a thought, perhaps it is this Warning: Cannot modify header information - headers already sent by (output started at /Users/user1/Documents/DEV/++htdocs/05_Debbie/index.php:22) in /Users/user1/Documents/DEV/++htdocs/05_Debbie/components/body_header.inc.php on line 48 As stated in the previous thread, and in the thread stickied at the top of this forum, you cannot send output to the browser before sending headers. You are includ()ing "body_header.inc.php" here <body> <div id="pageWrapper" class="clearfix"> <div id="pageInner"> <!-- BODY HEADER --> <?php require_once('components/body_header.inc.php'); ?> <!-- LEFT COLUMN --> <div id="pageLeftCol"> Note that the include() is embedded within output content. Then in the "body_header.inc.php" script you have this: if (mysqli_stmt_affected_rows($stmt1)!==1){ // Update Failed. $_SESSION['resultsCode'] = 'MEMBER_UPDATE_FAILED_2126'; // Redirect to Display Outcome. header("Location: " . BASE_URL . "members/results.php"); // End script. exit(); }//End of UPDATE MEMBER RECORD So, whenever index.php includes "body_header.inc.php", if the above condition is true it will attempt to execute that header() statement and it will always fail because output was already started back in index.php. Which is what the error message was stating. Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 There is a sticky on this board titled HEADER ERRORS - READ HERE BEFORE POSTING THEM. Go into that post and read it. It will give you valuable information about this. I have already read that Sticky... My problem may be obvious to you all - I hope so! - but I have been pouring over all of my files since last night and I just can't seem to find out where I am going wrong?! Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 At what stage are you including body_header.inc.php? I would hazard a guess as to it being before your session_start() statement thus giving you the error. In index.php, on line 3 there is this... // Initialize a session. session_start(); And then down on line 31 there is this... <!-- BODY HEADER --> <?php require_once('components/body_header.inc.php'); ?> Debbie Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 My problem may be obvious to you all - I hope so! - but I have been pouring over all of my files since last night and I just can't seem to find out where I am going wrong?! Just a wild guess: line 22 of index.php perhaps? Warning: Cannot modify header information - headers already sent by (output started at /Users/user1/Documents/DEV/++htdocs/05_Debbie/index.php:22) in /Users/user1/Documents/DEV/++htdocs/05_Debbie/components/body_header.inc.php on line 48 As already detailed above you include() the 2nd script after you have already sent content to the browser. So, when you try to run a header() in that 2nd script it will fail. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 I've been sitting here thinking you've got a problem with your sessions... No idea why, shows how tired I am. Psycho got it in 1. The code your executing in that body_header file can just be moved to the top of your index.php page... Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 Just a thought, perhaps it is this Warning: Cannot modify header information - headers already sent by (output started at /Users/user1/Documents/DEV/++htdocs/05_Debbie/index.php:22) in /Users/user1/Documents/DEV/++htdocs/05_Debbie/components/body_header.inc.php on line 48 Except Line 22 is this... <link type="text/css" rel="stylesheet" href="css/_main.css" /> I don't see what that has to do with anything?! As stated in the previous thread, and in the thread stickied at the top of this forum, you cannot send output to the browser before sending headers. I get that, but that doesn't mean I see where I am going wrong?! (Sometimes what is obvious to others is not to one's self!) Maybe on Release #3 - next release - I can start learning some OOP and learn to better separate my "Presentation Layer" from my "Business Layer" and problems like this will disappear!! But in the mean-time, my code is admittedly spaghetti-ish in places because I am mixing HTML and PHP. Hey, I'm only a newbie... You are includ()ing "body_header.inc.php" here <body> <div id="pageWrapper" class="clearfix"> <div id="pageInner"> <!-- BODY HEADER --> <?php require_once('components/body_header.inc.php'); ?> <!-- LEFT COLUMN --> <div id="pageLeftCol"> Note that the include() is embedded within output content. Like I said above, I know my code layout could be better, but for someone new to PHP, I think it is pretty good. Then in the "body_header.inc.php" script you have this: if (mysqli_stmt_affected_rows($stmt1)!==1){ // Update Failed. $_SESSION['resultsCode'] = 'MEMBER_UPDATE_FAILED_2126'; // Redirect to Display Outcome. header("Location: " . BASE_URL . "members/results.php"); // End script. exit(); }//End of UPDATE MEMBER RECORD Right, I see that is where my code is failing in NetBeans. SIDE TOPIC: Not sure why my Prepared Statement is failing, but when it does, it is causing my current issue as well?! So, whenever index.php includes "body_header.inc.php", if the above condition is true it will attempt to execute that header() statement and it will always fail because output was already started back in index.php. Which is what the error message was stating. I see what you are saying, but am not totally seeing it... In index.php, do things like <title> count as "output"?? Because I do not see any output here... <?php //Build Date: 2012-03-08 // Initialize a session. session_start(); // Access Constants. require_once('config/config.inc.php'); // Set current Script Name. $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME']; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- ################## DEBBIE ##################### --> <!-- HTML Metadata --> <title>Double Dee, Inc.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- Page Stylesheets --> <link type="text/css" rel="stylesheet" href="css/_main.css" /> <link type="text/css" rel="stylesheet" href="css/_layout.css" /> <link type="text/css" rel="stylesheet" href="css/top_menu.css" /> <link type="text/css" rel="stylesheet" href="css/components.css" /> </head> <body> <div id="pageWrapper" class="clearfix"> <div id="pageInner"> <!-- BODY HEADER --> <?php require_once('components/body_header.inc.php'); ?> Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 I've been sitting here thinking you've got a problem with your sessions... No idea why, shows how tired I am. Psycho got it in 1. The code your executing in that body_header file can just be moved to the top of your index.php page... I'm still trying to see where there are spaces at?! And the reason I put that "Update Last Activity" code in my Header is because I need it for every page on my website, so I figured it was smarter to put it in the Header. Am looking at my code again to look for those white spaces and see if I can design my code better WITHOUT having to re-code everything for Release #2... Debbie Quote Link to comment Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 Okay if you want something on every single page then simple create an entirely new inc file and call it "updateActivity.inc.php" include it at the very top of each page. Bobs your uncle, fanny's your aunt... Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 Still trying to chase down the *real* issue... In log_in.php I have... // Normal Redirect. if (isset($_SESSION['returnToPage'])){ header("Location: " . BASE_URL . $_SESSION['returnToPage']); }else{ // Take user to Home Page. header("Location: " . BASE_URL . "index.php"); } When you redirect to a new page, how does that affect the Header? If that code redirected me to index.php, then would anything have been "sent to the Header" once you are on index.php itself? Debbie Quote Link to comment Share on other sites More sharing options...
kicken Posted March 9, 2012 Share Posted March 9, 2012 In index.php, do things like <title> count as "output"?? Output is, roughly - Anything you echo or print - Anything sent by functions like print_r, var_dump, readfile, etc. - Anything that is outside of <?php ?> code blocks - Any error messages generated by PHP and sent to the browser (Warnings, notices, etc) Ultimately, if you can see it in your browser it is output. Anything you have to do prior to output such as use header() or call session_start() has to occur in your code prior to any of this. Once you have even a single byte of output, you can no longer use those functions. Because I do not see any output here... Output is this <?php //Build Date: 2012-03-08 // Initialize a session. session_start(); // Access Constants. require_once('config/config.inc.php'); // Set current Script Name. $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME']; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- ################## DEBBIE ##################### --> <!-- HTML Metadata --> <title>Double Dee, Inc.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- Page Stylesheets --> <link type="text/css" rel="stylesheet" href="css/_main.css" /> <link type="text/css" rel="stylesheet" href="css/_layout.css" /> <link type="text/css" rel="stylesheet" href="css/top_menu.css" /> <link type="text/css" rel="stylesheet" href="css/components.css" /> </head> <body> <div id="pageWrapper" class="clearfix"> <div id="pageInner"> <!-- BODY HEADER --> <?php require_once('components/body_header.inc.php'); ?> Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 Output is this How? As HTML? Or echo as PHP? I don't follow you... Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 If I put this into a blank PHP file and run it I get a white screen and I see "Double Dee, Inc." in the Page Title... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- ################## DEBBIE ##################### --> <!-- HTML Metadata --> <title>Double Dee, Inc.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- Page Stylesheets --> <link type="text/css" rel="stylesheet" href="css/_main.css" /> <link type="text/css" rel="stylesheet" href="css/_layout.css" /> <link type="text/css" rel="stylesheet" href="css/top_menu.css" /> <link type="text/css" rel="stylesheet" href="css/components.css" /> </head> <body> <div id="pageWrapper" class="clearfix"> <div id="pageInner"> <!-- BODY HEADER --> </div> </div> </body> </html> Debbie Quote Link to comment Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 Debbie Let me put it a little simpler for you; but it is essentially exactly what your doing. <?php // Your PHP code starts here session_start(); $var = ""; $var2 = ""; // BLAH BLAH // PHP ends ?> <html> <head> <!-- SOME HEADER STUFF --> </head> <body> <div>Some content</div> <? // You INCLUDE a file here which ends up executing a line very similar to the below line. // This tries to change the location HOWEVER, HTML has been output above ^^^^^^^^^^ header("location: something.php"); // This line is executed in your inc file. ?> </body> <html> See how HTML was output before the "header(location: blah)" line? Well your header redirect is inside your included file therefore HTML has been output to the browser already thus giving an error. Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 9, 2012 Author Share Posted March 9, 2012 Debbie Let me put it a little simpler for you; but it is essentially exactly what your doing. <?php // Your PHP code starts here session_start(); $var = ""; $var2 = ""; // BLAH BLAH // PHP ends ?> <html> <head> <!-- SOME HEADER STUFF --> </head> <body> <div>Some content</div> <? // You INCLUDE a file here which ends up executing a line very similar to the below line. // This tries to change the location HOWEVER, HTML has been output above ^^^^^^^^^^ header("location: something.php"); // This line is executed in your inc file. ?> </body> <html> See how HTML was output before the "header(location: blah)" line? Well your header redirect is inside your included file therefore HTML has been output to the browser already thus giving an error. I see what you are saying. I just got closer to figuring things out. But to clarify, I am not sending White Space to the browser, so that shouldn't be the issue. On problem that I discovered was in log_in.php where I forgot to add exit() in my IF-THEN-ELSE... // Normal Redirect. if (isset($_SESSION['returnToPage'])){ header("Location: " . BASE_URL . $_SESSION['returnToPage']); //NEW // End script. exit(); }else{ // Take user to Home Page. header("Location: " . BASE_URL . "index.php"); //NEW // End script. exit(); } That was causing my code to drop down to the HTML section and display output which caused the error. Now that I have that fixed, I still have the double re-direct issue to which I believe you are mentioning above. If I understand where my code is failing... I am in "log_in.php" and submit the Form. Everything goes fine, and my script redirects to the appropriate page - see code above - and all should be fine. HOWEVER, when I get to the "returnToPage" that page includes my "body_header.inc.php" file which has this code... // ************************ // Update Member Record. * // ************************ // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); // Build query. $q1 = "UPDATE member SET logged_in=?, last_activity=now() WHERE id=? LIMIT 1"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variables to query. mysqli_stmt_bind_param($stmt1, 'ii', $loggedIn, $memberID); // Execute query. mysqli_stmt_execute($stmt1); // Verify Update. if (mysqli_stmt_affected_rows($stmt1)!==1){ // Update Failed. $_SESSION['resultsCode'] = 'MEMBER_UPDATE_FAILED_2126'; //echo 'HERE'; // Redirect to Display Outcome. header("Location: " . BASE_URL . "members/results.php"); // End script. exit(); }//End of UPDATE MEMBER RECORD So if for some reason I was unable to update the Member record with a new "last_activity" then I want to redirect to my Error Page but that apparently won't work because I already redirected to the page I am on... At least I think that is what is going on?! Am I making any progress here?? Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 9, 2012 Share Posted March 9, 2012 You can send NO OUTPUT before sending headers, and HTML IS OUTPUT. Quote Link to comment Share on other sites More sharing options...
cpd Posted March 9, 2012 Share Posted March 9, 2012 Debbie Outline step by step what you want to do. E.g. Index -> Submit Login -> Redirect to loggedin.php if successful... Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 9, 2012 Share Posted March 9, 2012 Add me to skype: RussellReal I could help you here, but that would be alot of typing, and I'm too fresh-awake for that, still haven't washed my face even LOL - Russell Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 I have already pointed out the EXACT problem and identified the exact sections of code that are causing it. Apparently, there is confusion on what "output" is. Sort of reminds me of It depends on what the meaning of the words 'is' is. In the index.php file there is a ton of OUTPUT before the body_header.inc.php script is included. Kicken already provided a version of the index.script with all the OUTPUT in red text. That OUTPUT is sent before the body_header.inc.php script is included. So, when the header() function is run in the body_header.inc.php script, it fails because some OUTPUT was already sent to the browser. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 Maybe this will help. file1.php <?php //There are empty lines in the file before the opening PHP tag //It is OUTPUT ?> This text is outside PHP tags. It is OUTPUT <?php echo "This text is in an echo statement. It is OUTPUT"; print_r($array); //This is OUTPUT //Include file2.php include('file2.php'); ?> file2.php <?php header("Location: http://somesite.com/"); ?> The header in file2.php will generate the same error you are having (when called from file1.php) because of all the instances of output in file1.php before file2.php is included Quote Link to comment 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.