0

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?

danwoods
  • 4,889
  • 11
  • 62
  • 90
  • Do you have an `auto_prepend_file` configured in `php.ini` or `.htaccess`? – gahooa Dec 12 '09 at 02:00
  • Are you including this file into some other file? – Nirmal Dec 12 '09 at 02:01
  • exactly! I'm calling this file to process a form created by another php file. If I don't use session_start() how should I start/continue the session then, or gain access to those session variables? – danwoods Dec 12 '09 at 02:06
  • Then you should run session_start() at the top of the file that includes this file. session_start() does not have to be run in every file, just once and before any output has been sent to the browser. – Tjofras Dec 12 '09 at 02:16
  • removed session_start from secondary file and now get Notice: Undefined variable: _SESSION errors – danwoods Dec 12 '09 at 02:35

3 Answers3

2

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
  • 57,995
  • 32
  • 132
  • 151
1

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
  • 2,116
  • 12
  • 13
1

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');
?>
leepowers
  • 37,828
  • 23
  • 98
  • 129