tags:

views:

76

answers:

3

Hello all, I'm getting a
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

error even though the first line of my file looks like:

<?php session_start();

all the answers I saw on other sites said it was from sending info to the server before starting the session. Why am I getting this error?

+1  A: 

check that there are no invisible characters before the <?php session_start(); using a hex editor. If you convert the file to UTF-8, then many editors insert BOM characters at the beginning, which are not visible in your editor, but do show up in a hex editor.

Marius
no invisible characters...
danwoods
+1  A: 

The absolute easiest way to get rid of all these problems is to enable output buffering.

Setting the value of output_buffering to On in php.ini

output_buffering = On
Tjofras
Why would the default of this be 'off'?
danwoods
There is probably some small performance penalty for keeping a buffer rather then just sending it right away.
Tjofras
+1  A: 

As noted, this error is caused when information is echo()'d, print()'d or otherwise sent to the web browser. You mentioned the session_start() is in an included file. Does the parent/containing file look anything like this?

<?php
  // Some processing code, etc.
?>
<html>
<body>
Hello here's some content
<?php include('session_starter.php');
</body>
</html>

Turning on output buffering in php.ini or via ob_start() at the top of parent/containing file will take care of this.

Also, check for trailing/leading newlines in the parent php file:

 <- There's a newline here, so output has started
<?php
 // Do some stuff
?> <- Newline here, output has started
<?php
 include('session_starter.php');
?>
pygorex1
the ob_start() worked thanks man
danwoods