Php warning in session test

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

I have the code above at http://dot.kr/x-test/page1.php which is from http://php.net/manual/en/function.session-start.php .
Why does it cause a warning?
How can I fix it?

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…

when I run your code on my local XAMPP server it workd without any errors or warnings.

can you post the warning message you get.

This error is very common and can be caused by one of two things:

  1. 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.
  2. 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.

See http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start

I only copied and pasted the posted code.

I didn’t click the link. I don’t normally click posted links for obvious security reasons.

The error is:

So it is MrWoosters #1 answer.
NO OUTPUT, nothing, nada, zip, diddly, 방언 The <?php must be the first characters on the page so no white space.

the error message says that you have output starting on page1.php at line 3.

Ah… thanks spikeZ… indeed, the error message says it all… headers already sent.

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 don’t understand the meaning of this.
Does it mean that header already sent the session?

I have still the warning message yet.
How can I remove the warning message?

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:

if it’s any help, I have this in my local php.ini

 
; Initialize session on request startup.
; http://php.net/session.auto-start
session.auto_start = 0

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).

that session id is coming from the paramater SID in the OP’s original posted code

 
<?php
// page1.php
 
session_start();
 
echo 'Welcome to page #1';
 
$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();
 
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';
 
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . [COLOR=red]SID[/COLOR] . '">page 2</a>';
?>

maybe try removing the red SID from the original code?

I have the code below at http://dot.kr/x-test/page1-2.php .
(It’s too long to post all the result of it)

<?php phpinfo() ?>

I have the code below which is very simple at http://dot.kr/x-test/page1-3.php .

<?php
session_start();
?>

It says like the following.

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

do you still get the warnings if you remove SID from your original php code?

I have the code below at http://dot.kr/x-test/page1-4.php .

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed 
echo '<br /><a href="page2.php?' [COLOR="Red"]. .[/COLOR] '">page 2</a>';
?>

It says like the following.

Parse error: syntax error, unexpected ‘.’ in C:\RWAPM\www\x-test\page1-4.php on line 18

change

 
echo '<br /><a href="page2.php?' . . '">page 2</a>';


to

 
 
echo '<br /><a href="page2.php">page 2</a>';


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 :frowning:

Hmmm

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:


<?php
echo session_id();
?>

The output of the code below is at http://dot.kr/x-test/page1-5.php . It says nothing (blank).

<?php
echo session_id();
?>
for webPage identity