Jump to content

session_start() warning


boylesg

Recommended Posts

What does this mean and how do I stop it?Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\smallplants.php:8) in C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\OnlineNurseryScripts.php on line 2Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\smallplants.php:8) in C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\OnlineNurseryScripts.php on line 2The call to session_start() is in onlinenurseryscripts.php like so:<?phpsession_start();And smallplants.php includes onlinenurseryscripts.php like so:<?phpinclude("OnlineNurseryScripts.php");

Link to comment
Share on other sites

What does this mean and how do I stop it?Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\smallplants.php:8) in C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\OnlineNurseryScripts.php on line 2Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\smallplants.php:8) in C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\OnlineNurseryScripts.php on line 2The call to session_start() is in onlinenurseryscripts.php like so:<?phpsession_start();And smallplants.php includes onlinenurseryscripts.php like so:<?phpinclude("OnlineNurseryScripts.php");
And why does a call to this: $SessionData = array_values($_SESSION); result in this error?Warning: array_values() [function.array-values]: The argument should be an array in C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\BuyItem.php on line 270
Link to comment
Share on other sites

session_start() has to be called before any output whatsoever. If you don't, then it won't work, hence why you can't access the $_SESSION superglobal later on.

Link to comment
Share on other sites

I keep wondering what's so hard to understand about this... but maybe it's just me, because I knew what an HTTP header is before learning about cookies and sessions (in this order... an important thing too).When you output anything from a PHP file, an HTTP response is sent. An HTTP response contains a status line (which alerts the browser if the whole process is OK or not, among other things), HTTP headers which your browser sees and uses for its own purposes, and a "body", which is basically whatever you're seeing on screen (well... in "View Source" to be more precise, but still...). The HTTP response MUST look exactly like that - status line, headers, body. Everything you don't see on screen (e.g. cookies) is actually an HTTP header. You can't add headers once the body starts.Data not enclosed in "<?php" and "?>" is also output.Look carefully at the error message, and tell me you don't see anything suspicious... here's a hint:

C:\Inetpub\vhosts\gregsindigenouslandscapes.com.au\httpdocs\temp\smallplants.php:8
If the first two lines of this file are indeed what you say... why is "<?php" on line 7, and the include line at line 8? Whitespace? HTML? Bingo! Remove that.[edit]Well... apparently, trying to teach you how to pinpoint the culprit made me post a little too much later than Synook and Deirdre's Dad.[/edit]
Link to comment
Share on other sites

Obviously, "any output whatsoever" includes any HTML that comes before your script. It also includes any spaces, tabs, and line breaks that might come before your <?php tag.
Yeah well I subsequently found that bit of info, i.e. that any output at all includes the html stuff as well as the php stuff.But my next problem is that session variables set in one php file cannot be accessed in the next php file (target of a post from the previous file where the session variables are set).Any suggestions here?
Link to comment
Share on other sites

Yeah well I subsequently found that bit of info, i.e. that any output at all includes the html stuff as well as the php stuff.But my next problem is that session variables set in one php file cannot be accessed in the next php file (target of a post from the previous file where the session variables are set).Any suggestions here?
why not? that's the whole point of $_SESSION. If its set in SESSION, why do you need to pass it?
Link to comment
Share on other sites

They should be. A couple things can go wrong. You must use session_start in every file that needs to access session data. And your URLs must use the same domain. I mean, if a session begins at www.example.com, a file at example.com cannot access the session data. It must be at www.example.com.

Link to comment
Share on other sites

Are you sure the session was started? Did you removed the error message(s) about the headers being already sent?$_SESSION is not initialized without a successful session_start() call.

You must use session_start in every file
Last time I checked, starting the session at any one point (e.g. an included file) makes it available to any further files included, current and even to any "parent" points below and after the start. So, in his case, smallplants.php should have access to $_SESSION after a successful session_start() call in OnlineNurseryScripts.php.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...