co.ador Posted January 19, 2010 Share Posted January 19, 2010 I have encounter this warning before Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/store/includes/connection.php:39) in /home3/store/includes/connection.php on line 30 Could I use the function ob_start(); before session star() and use it anywhere I want? or do I have to star it before any out any ways? I once used ob _start() function before a header() and it worked. It started the hearder even though there were other outputs before the header() function. Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/ Share on other sites More sharing options...
Lamez Posted January 19, 2010 Share Posted January 19, 2010 just add ob_start(); at the top as a temporary fix. You are sending out HTML before calling the session_start() function. Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998137 Share on other sites More sharing options...
co.ador Posted January 19, 2010 Author Share Posted January 19, 2010 <?php header('Content-type: text/html; charset=utf-8');?> <?php session_start(); function GetCartId() { if(isset($_COOKIE["cartId"])) { return $_COOKIE["cartId"]; } else { setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); //line 11 return session_id(); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Warning: Cannot modify header information - headers already sent by (output started at /home3/includes/header3.php:24) in /home3/includes/header3.php on line 11 It seem that it setcookie function has a problem when it is not started at the top as well. Because the session is at the top and it is not throwing any error but there is a warning for line 11 where the function setcookie() is. But how can I put it at the top wherer the else statment won't have any effect on it? I think ob_start would be of a good use right? where exactly should I use ob_start(); here or how can I take that warning out of the way? Right now it is working but the warning doesn't look too pretty in the screen. Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998246 Share on other sites More sharing options...
KevinM1 Posted January 19, 2010 Share Posted January 19, 2010 Once again, the sticky topic that describes the "Headers already sent" error and how to fix it: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998248 Share on other sites More sharing options...
co.ador Posted January 19, 2010 Author Share Posted January 19, 2010 If you notice the setcookie and session_star() functions are before the html output, UCH! Don't know how it still displaying the error, where they should be hidden! Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998257 Share on other sites More sharing options...
co.ador Posted January 19, 2010 Author Share Posted January 19, 2010 I have used ob_start(); as a fixt by! it took out the messages out! Thank you! I knew I didn't had any space but there should be something wrong but by now I will get by with ob_start(); Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998258 Share on other sites More sharing options...
piyush23424 Posted January 19, 2010 Share Posted January 19, 2010 i think you can also use to @session_start() instead of session_start() to supress the warning error Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998334 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2010 Share Posted January 19, 2010 All that would do is to hide the fact that the session start is failing. Your code won't work and you will not have any indication why. Hiding the error message DOES NOT fix the error. Quote Link to comment https://forums.phpfreaks.com/topic/189043-a-warning-using-session-start-again/#findComment-998337 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.