PHP header() call “crashing” script with HTTP 500 error [duplicate]

与世无争的帅哥 提交于 2019-12-07 02:03:29

No, those lines are not equivalent. The first will write an invalid header line. The documentation to the header function says:

header() is used to send a raw HTTP header.

And in the description of the string parameter:

There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send.

In your case the value Not Modified does not start with HTTP/ and thus is not treated to set the HTTP status code but just a regular header field. But it is invalid as it does not has the form:

message-header = field-name ":" [ field-value ]

And that’s what the error log entry states. Not Modified is not a valid raw HTTP header.

The third parameter http_response_code is just to set the status code while setting a redirection to avoid two header calls like:

header('HTTP/1.1 301 Moved Permanently');
header('Location: http://example.com');

Instead of that you can simply write:

header('Location: http://example.com', true, 301);

You will want to check your web server error log, not your PHP error log in this case, to get more information about the crash.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!