Other than white space what else can trigger “head

2019-08-30 03:00发布

问题:

It's a header already sent question but the issue I have is that I have a script which I run locally (easyPHP on Win) and on my live server (LAMP).

This works fine on both and I do not get the header error.

Now I have a client who runs it on his server and he's getting the header error.

I'm pretty sure the issue is one of the files (I've just sent him one with no break lines after "?>" - that can be an issue right?) but I have no idea why he's getting the error but I'm not - even with the whitespace after "?>" (yes, my error reporting is on)

Is there something other than white spaces that can cause this error?

Quick edit, having read the question linked below

First error:

<b>Warning</b>:  session_regenerate_id() [<a href='function.session-regenerate-id'>function.session-regenerate-id</a>]: Cannot regenerate session id - headers already sent in <b>bla bla bla/includes/lightwork_session.php</b> on line <b>33</b><br />

Line 33 in "lightwork_session" is: I don't think this is the issue

public static function sessionStart(){
//regenerate the session id to prevent session fixation
session_regenerate_id(true); //hello, I'm line 33
session_start(); //hello again, I'm line 34 in

}

Next batch of errors:

  <b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at bla bla blaconfig/config_global.php:167) in <b>bla bla bla/includes/lightwork_session.php</b> on line <b>34</b><br />
<br />

<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at bla bla bla/config/config_global.php:167) in <b>bla bla bla/includes/lightwork_session.php</b> on line <b>34</b><br />

Okay line 167 in config_global.php is white space just after "?>"

I've sent him a file without that whitespace but being on the other side of the world I'll find out tomorrow how that works out for him. But I'm stumped as to why he gets the error and I do not?

回答1:

White space after the closing tag is one problem, but another common reason this happens is due to PHP warnings and notices being turned to a higher reporting level on his server. If you have any warnings or notices from PHP that get printed out this will cause the headers warning. You should turn off notices on his server using:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

or to have only errors:

error_reporting(E_ERROR);

I would recommend leaving E_WARNING on and fixing the issues that are reported, but at least it will tell you if that is the problem.



回答2:

In addition to @davidethell, I'd like to add that it's also possible the error is caused by line break types or Unicode signatures. This is an option in a lot of IDE's and other editors (see the 'New Document' and 'Code Format' section in Dreamweaver for example). To be safe, always go for Unix styled line break types (LF instead of CR LF) and don't include the Unicode signature (BOM).

Also, a general tip when working with large PHP files: you don't have to include the closing ?> tag. CodeIgniter for example adopted this practice, they just "close" their files with a comment, this prevents accidental whitespace after the closing php tag.



回答3:

Some cases would be as follows:

  1. Whitespace before the opening php tag <?php
  2. Sending something to the browser before using session_start, header, setcookie etc..
  3. UTF BOM (byte order mark) problem..

If you want a quick fix....take a look at ob_start() in PHP....although using ob_start to suppress errors of this kind is a stopgap arrangement.

Hope this helps..