开发者

PHP headers already sent error [duplicate]

开发者 https://www.devze.com 2022-12-13 17:26 出处:网络
This question already has answers here: 开发者_JAVA技巧 How to fix "Headers already sent" error in PHP
This question already has answers here: 开发者_JAVA技巧 How to fix "Headers already sent" error in PHP (11 answers) Closed 9 years ago.

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?


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 
0

精彩评论

暂无评论...
验证码 换一张
取 消