Session warning

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EltonSky
Forum Newbie
Posts: 7
Joined: Tue May 02, 2006 7:47 pm

Session warning

Post by EltonSky »

Hello guys,
When I try to use session_start(), I got warning :
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/c/ctian/.HTMLinfo/learn/sessTest.php:2) in /home/c/ctian/.HTMLinfo/learn/sessTest.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/c/ctian/.HTMLinfo/learn/sessTest.php:2) in /home/c/ctian/.HTMLinfo/learn/sessTest.php on line 3

Can anybody tell me what does it mean, how can i handle them?

Cheers,

Elton
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

It means you have already sent headers to you browser...

Don't echo or have any HTML code before those function calls...either that or buffer your output...

Cheers :)
EltonSky
Forum Newbie
Posts: 7
Joined: Tue May 02, 2006 7:47 pm

Post by EltonSky »

Thanx for reply,

Do u mean my session is working and I should ignore that warning ? I use session_start() at the very beginning of the page actually.
Can u clarify a bit ?

REGARDS,

Elton
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to make sure that absolutely nothing, not even a blank white space, is sent to the browser before the call to session_start(), unless you are using output buffering. If anything gets sent to the browser before a call to session_start, you get the headers already sent warning.

PS, Warnings are not for ignoring. If you get one, fix it. Then continue to code in peace knowing that you are awesome for not cutting corners or being lazy.
EltonSky
Forum Newbie
Posts: 7
Joined: Tue May 02, 2006 7:47 pm

Post by EltonSky »

Hi Everah,

Thank u.
This what i did:
<?php session_start();
?>
<HTML>
<HEAD>
<title>
.....

I think it 's already on the top, right?


REGARDS,

Elton
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

As long as your code starts like this...

Code: Select all

<?php
session_start();
// continue on...
?>
... and it is not being included in another file, you should be fine. However, take a look at the next two pieces of code and see if you can spot the problem...

Code: Select all

  <?php
session_start();
// continue on...
?>

Code: Select all


<?php
session_start();
// continue on...
?>
Even that white space at the beginning of the PHP tag causes problems. If you are certain that your session call is appropriately placed, then the problem might not be code but PHP configuration or server configuration related.
Post Reply