views:

183

answers:

3

I just started making this .php file and i cant seem to figure out why its throwing this warning

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampplite\htdocs\projectchest\controlpanel\index.php:1) in C:\Program Files\xampplite\htdocs\projectchest\controlpanel\index.php on line 2

Here is my php.

<?php
    session_start();
    require_once('../classes/users/BaseUser.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   ...
</html>

And here is access to my php.ini file

pastebin dot com / f60d22891 sorry i cant post two links cuz of some spam nonsense

i also found this thread suggested that my permission settings are messed up. i tried this...no luck http://stackoverflow.com/questions/670595/php-sessionstart-fails

+4  A: 

Sessions in PHP are based on a cookie (to transmit the session_id).

Cookies are sent as HTTP headers.

HTTP headers can only be sent if no output has already been sent.

The error message you're getting indicates that the session cookie cannot be sent because some output has already been sent.


Which means you must be sure not to send any ouput (not even one white-space !) before calling session_start().


A couple of ideas :

  • make sure there is no blank line before the opening php tag
  • make sure your PHP script is not encoded in UTF-8 with a BOM (some editor put that by default at the beginning of the files)
    • i.e. re-save your file (as UTF-8, why not), without a BOM


As a sidenote : you might get this error on some servers, and not on some others, because of the configuration of output_buffering : if it's on (it's Off in your php.ini file), PHP will keep some data in memory before sending them -- which means headers can be sent even if a small amount of data seems to have already been sent (In reality, that small amount of data is kept in memory, and not immediatly really sent).

Pascal MARTIN
I think you mean 'make sure there is no blank line' :-)
richsage
@richsage : ergh :-D yes, of course ^^ Thanks for your comment :-) (I edited my post to correct that mistake)
Pascal MARTIN
that did it sweet! :) i have been scratching my head for hours.
cr1ms0n3cho
@Anthony don't forget to accept the answer if you think it solved your problem ;-)
richsage
@Anthony : an external help often does wonders ;-)
Pascal MARTIN
+1  A: 

Most likely a character encoding issue (UTF 8 BOM to be precise). Try converting your php source file into UTF-8 with no BOM. Which editor are you using?

code_burgar
I am using notepad++
cr1ms0n3cho
Format -> Convert to UTF-8 no BOM :)
code_burgar
A: 

There is also the session.auto_start directive in php.ini (see manual) which will take care of exchanging cookies and setting up the session as soon as PHP sends the HTTP headers. Not as customizable, but it's the least-distance solution.

amphetamachine