you can write echo after assigning the session variables…
Session will start before you start display on browser, after the display statement it shows warning…
This error is very common and can be caused by one of two things:
Some output has already been sent to the browser. If this is the case then you cannot then set a session. This can occur if you use and command that sends output to the browser (eg echo) before calling session_start();. The reason is that the session details are sent in the HTTP header… calling echo forces you script to send a header.
The session has already been started. Sessions can be set to auto start in you php.ini file. If this is the case, session_start() will throw an error because the session has already been started.
I have not tested your code but since Kalon has managed to run it without errors, I am tending more towards option 2.
I think I’ve learned much from you. I’ll post the warning message from the next time.
The following is the full message.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\\RWAPM\\www\\x-test\\page1.php:3) in C:\\RWAPM\\www\\x-test\\page1.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\\RWAPM\\www\\x-test\\page1.php:3) in C:\\RWAPM\\www\\x-test\\page1.php on line 6
Welcome to page #1
page 2
page 2
I have copied and pasted your posted php code into a file and ran it on both my local XAMPP and “real world” servers without any errors or warnings at all.
Your code looks fine to me and I don’t see anything in your code that should give those warnings.
Since I’m not getting those warnings, I’m starting to think it’s a server configuration issue on your server but unfortumately I have no idea what it could be.
perhaps mrwooster’s option 2 is on the right track :confused2:
Hmmm… that means that the session is ‘not’ auto started.
However… looking at the source for the code:
<br />
<b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at C:\\RWAPM\\www\\x-test\\page1.php:3) in <b>C:\\RWAPM\\www\\x-test\\page1.php</b> on line <b>6</b><br />
<br />
<b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at C:\\RWAPM\\www\\x-test\\page1.php:3) in <b>C:\\RWAPM\\www\\x-test\\page1.php</b> on line <b>6</b><br />
Welcome to page #1<br /><a href="page2.php">page 2</a><br /><a href="page2.php?PHPSESSID=a55a0606e8af6462d7aeaeb702611c32">page 2</a>
You can see that the phpsess id is being passed… meaning that the session has actually been started.
Can you put a phpinfo(); in your script and do a search for ‘session’, see if auto start is still false (it might be set to true somewhere else in the configs).
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\RWAPM\www\x-test\page1-3.php:1) in C:\RWAPM\www\x-test\page1-3.php on line 2
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\RWAPM\www\x-test\page1-3.php:1) in C:\RWAPM\www\x-test\page1-3.php on line 2
which is basically the same as your 1st link in your code and so it shouldn’t give you any errors.
if it doesn’t then at least we can confirm the warnings are related to you using the SID parameter but why I get no warnings and you do unfortunately I do not know
This is really strange. The phpinfo clearly shows that the session is no auto started but all the errors seem to indicate that the session has already been started.
Can you put the following code in a file and run it and let me know what the output is: