PHP headers already sent error [duplicate]
I'm getting a
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already senterror 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?
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.
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');
?>
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
精彩评论