Christopher Bergin Posted January 17, 2013 Share Posted January 17, 2013 I was trying to modify the database queries in exercise 3.1 that created and destroyed sessions. The author uses the sprintf function to invoke his queries. I tried to use a standard query and I'm receiving 'header already sent' errors. the query used in book appears like this: $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid)); the query I was attempting to use was: $q= 'SELECT data FROM sessions WHERE id= $sid'; the book: $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")', mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data)); mine: $q= 'REPLACE INTO sessions (id, data) VALUES ($sid, $data)'; the error I'm receiving is the following: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/bosweb/web108/b1084/ipg.cjbergincom/PHP test/db_sessions.inc.php:18) in /hermes/bosweb/web108/b1084/ipg.cjbergincom/PHP test/db_sessions.inc.php on line 91 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web108/b1084/ipg.cjbergincom/PHP test/db_sessions.inc.php:18) in /hermes/bosweb/web108/b1084/ipg.cjbergincom/PHP test/db_sessions.inc.php on line 91 line 91 is the session_start command. line 18 is a conditional statement in the read_session function... function read_session($sid) { global $sdbc; // Query the database: //$q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid)); $q= 'SELECT data FROM sessions WHERE id= $sid'; $r = mysqli_query($sdbc, $q); // Retrieve the results: if (mysqli_num_rows($r) == 1) { list($data) = mysqli_fetch_array($r, MYSQLI_NUM); // Return the data: return $data; } else { // Return an empty string. return ''; } } // End of read_session() function. If it helps to present the entire script, let me know. Link to comment Share on other sites More sharing options...
HartleySan Posted January 18, 2013 Share Posted January 18, 2013 Not sure if this is the issue, but you're using single quotes for your query strings, meaning that the variables (e.g., $sid) are being interpreted as literal strings and not values. This will likely cause your queries to fail. I'm not sure if this is related to the header issue, though. Are you setting headers anywhere in your file? Link to comment Share on other sites More sharing options...
Edward Posted January 18, 2013 Share Posted January 18, 2013 You can try the headers_sent() function if it returns boolean it means something was already sent to the web browsers. The headers already sent could also be the result of a php closing tag, that script 3.1 you are modifying is an include file, check the closing tag on that if there is one remove it. Also another thing you probably already know just a reminder here is about the output buffering that can be useful in stopping the headers already sent. Check out ob_start(), ob_end_flush() and ob_end_clean(). Link to comment Share on other sites More sharing options...
Christopher Bergin Posted January 18, 2013 Author Share Posted January 18, 2013 thank you both. Hartley was right, I failed to include quotes in my query. I'll have to go back and review what constitutes a string and what constitutes a variable. Link to comment Share on other sites More sharing options...
Recommended Posts