GuyFreakz Posted November 9, 2008 Share Posted November 9, 2008 Hi guys, I'm a student who learns php. And now I'm learning about session. I face a problem when i started session with session_start(). First, i declared the "session_start" in page1.php like this <?php session_start(); $_SESSION["username"]="Kay"; ?> And after that i running my opera browser and there is no problem when i entered http://localhost/practice/page1.php. But then i make another page2.php <?php session_start(); $user = $_SESSION["username"]; echo $user; ?> and got these messages: ------------------------------------------------------------------------- Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\practice\page2.php:1) in C:\xampp\htdocs\practice\page2.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\practice\page2.php:1) in C:\xampp\htdocs\practice\page2.php on line 2 Kay ------------------------------------------------------------------------- I've tried to close the browser but still the messages appeared. Then i copy the page2.php to another folder, and when i opened again there is no problem. My question is where is my fault? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/ Share on other sites More sharing options...
.josh Posted November 9, 2008 Share Posted November 9, 2008 The error tells you the problem. You cannot have output before starting a session. Make sure there is no output, not even blank lines or whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-686313 Share on other sites More sharing options...
corbin Posted November 9, 2008 Share Posted November 9, 2008 http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-686315 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 Make sure you do not have an enter before the <?php IE: WRONG <?php //code here ?> RIGHT <?php // code here ?> Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-686331 Share on other sites More sharing options...
GuyFreakz Posted November 11, 2008 Author Share Posted November 11, 2008 Make sure you do not have an enter before the <?php IE: WRONG <?php //code here ?> RIGHT <?php // code here ?> I've checked it many times and there is not even 1 space or tab. I'm not sure but after i followed the link given by corbin, they tell me that the trouble may lays on the text editor. I usually used Notepad++, but i've created the page1.php using TextPad and just for fun i made the second used notepad++. After that i copy the content of page2.php to new document i created in textpad and save it there but in different document. So my deduction is there is error in Notepad++, when it create a new document, just maybe, it's automatically add an invisible character at the beginning of document. Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-687601 Share on other sites More sharing options...
radalin Posted November 11, 2008 Share Posted November 11, 2008 It's possible that \r (new line for most ms editors) or \n (newline for rest of the world:) might be added with some editors. These are invisible characters, these characters are recognised by the editor and shown. When you open a .txt file with vi you see strange chars like ^M, this one refers to "\r". This might be the problem. I had encountered this problem too. Use an editor which you trust Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-687602 Share on other sites More sharing options...
BioBob Posted November 11, 2008 Share Posted November 11, 2008 If its the text editor you are using, skip newline or \r or \n and just put it all on one line... <?php session_start(); Then plug in your code from there. You can also try OUTPUT BUFFERING (OB) but it may cause a performance hit. <? ob_start(); session_start(); ob_end_flush(); $user = $_SESSION['user']; echo $user; Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-687614 Share on other sites More sharing options...
GuyFreakz Posted November 11, 2008 Author Share Posted November 11, 2008 It's possible that \r (new line for most ms editors) or \n (newline for rest of the world:) might be added with some editors. These are invisible characters, these characters are recognised by the editor and shown. When you open a .txt file with vi you see strange chars like ^M, this one refers to "\r". This might be the problem. I had encountered this problem too. Use an editor which you trust Ho... so it can be like that huh?! I see that's explained why my code always generates error. Thx guyz . Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-687760 Share on other sites More sharing options...
PFMaBiSmAd Posted November 11, 2008 Share Posted November 11, 2008 When the error refers to output occurring on line 1 and you have checked and removed any possible content before the <?php tag, then the error is most likely due to the page being saved in UTF-8 encoding and the BOM (Byte Order Mark) characters that your editor places at the start of the file is the problem. Either save your file as ANSI/ASCII or if you must save it as UTF-8, pick the option in your editor to save it without the BOM (or get a different editor.) Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-687777 Share on other sites More sharing options...
GuyFreakz Posted November 11, 2008 Author Share Posted November 11, 2008 When the error refers to output occurring on line 1 and you have checked and removed any possible content before the <?php tag, then the error is most likely due to the page being saved in UTF-8 encoding and the BOM (Byte Order Mark) characters that your editor places at the start of the file is the problem. Either save your file as ANSI/ASCII or if you must save it as UTF-8, pick the option in your editor to save it without the BOM (or get a different editor.) You're right!!! That's the best solution. After i changed the setting of my notepad++, there is no error anymore. Thank you very much. Thank you!!! :D :D Quote Link to comment https://forums.phpfreaks.com/topic/132073-solved-trouble-when-declaring-session_start/#findComment-687897 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.