PHP header()调用“崩溃”具有HTTP 500错误的脚本 [英] PHP header() call "crashing" script with HTTP 500 error

查看:336
本文介绍了PHP header()调用“崩溃”具有HTTP 500错误的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP 5.2.9。

要查看/记录错误,在脚本的开始处我有:

  error_reporting(E_ALL | E_STRICT); 
ini_set('display_errors','On');
ini_set('log_errors','On');
ini_set('error_log',$ _SERVER ['DOCUMENT_ROOT']。'/ php.log');

但是,当我使用下两行中的第一行时,脚本发出HTTP 500错误,没有任何显示(500错误除外),并没有记录在我的php.log。如果我使用第二行而不是第一行,则一切正常,并且304正常发行。

  header('Not修改',真,304); // 500错误
header($ _ SERVER ['SERVER_PROTOCOL']。'304 Not Modified',true,304); // Works OK
//在两种情况下exit();在header()之后被调用。

这两行是否相同(请参阅下面的链接)?为什么第一个版本崩溃我的脚本?!



这与



编辑1:这是我可以找到的唯一日志:

 '
69.70.84.xx - - [20 / Jan / 2010: 15:54:06 -0600]GET / framework2 / HTTP / 1.1200 2968 - Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:08 -0600]GET /foo/bar.js HTTP / 1.1 404 8642http://www.mydomain.com/framework2/Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:10 -0600]GET / framework2 / HTTP / 1.1500 8511 - Mozilla / 5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:11 -0600]GET / frame work / mydomain_ajax-dom.js HTTP / 1.1304 - http://www.mydomain.com/framework2/Mozilla / 5.0(Windows; U; Windows NT 5.1; EN-US; rv:1.9.1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:11 -0600] GET /framework/templates/common/Reset.css HTTP / 1.1304 - http://www.mydomain.com/framework2/Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv: 1.9.1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:11 -0600]GET / framework / templates / common / Error.css HTTP / 1.1304 - http://www.mydomain.com/framework2/Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.9。 1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:11 -0600]GET / framework / templates / common / Base.css HTTP / 1.1304 - http://www.mydomain.com/framework2/Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
69.70.84.xx - - [20 / Jan / 2010:15:54:11 -0600]GET / images / Error- 48.png HTTP / 1.1304 - http://www.mydomain.com/framework2/Mozilla / 5.0(Windows; U; Windo ws NT 5.1; EN-US; rv:1.9.1.7)Gecko / 20091221 Firefox / 3.5.7(.NET CLR 3.5.30729)
'




  1. 第1行是正确提供的文档。

  2. 第2行是我的文档中的虚拟.js的404。 >
  3. 第3行是发生实际错误的地方(我在我的浏览器中刷新了刷新信息,我的PHP发出304头文件使我的脚本崩溃)。
  4. 第4行, 5,6,7,8是由显示500错误消息的自定义错误消息(通过.htaccess)引起的。

Edit 2:我联系了Web主机支持人员,询问我是否可以访问更多的日志,他们表示否,但是他们有权访问我无法看到的日志,我的错误在那里:

 'backend:来自脚本的格式不正确的标题Bad header = Not Modified:index.php'

回到我的问题......为什么在某些主机上,第一个版本不起作用,而其他版本似乎没问题上面的链接表示seco nd崩溃,而第一个是OK)

解决方案

不,这些行不相等。第一个会写一个无效的标题行。 标题函数文档说:


header()用于发送原始HTTP标头。 $ b

string 参数的描述中:


有两种特殊情况的标题调用。首先是一个以字符串HTTP /开头的头文件(大小写不重要),该头文件将用于计算要发送的HTTP状态码。


在你的情况下, Not Modified 的值不是以 HTTP / 开头,不处理设置的HTTP状态代码,但只是一个普通的标题栏。但它是无效的,因为它没有形式:

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

这就是错误日志条目的状态。 未修改不是有效的原始HTTP标头。 第三个参数 http_response_code 是只需在设置重定向时设置状态码以避免两个调用,如:

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

相反,您可以简单地写:

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


I use PHP 5.2.9.

To see/log errors, at the beggining of my script I have:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
ini_set('log_errors', 'On');
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'] . '/php.log');

But when I use the first of the next two lines, my script issue a HTTP 500 error, and nothing is displayed (except the 500 error) and nothing is logged in my php.log. If I use the second line instead of the first one, all is OK and the 304 is issued normally.

header('Not Modified', true, 304); //Crash php with 500 error
header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304); //Works OK
//In both case exit(); is called just after header().

Aren't these two lines equivalent (see link below)? Why the first version "crashes" my script?!

This is related to: Header use in PHP

Edit 1: This is the only log I can find:

'
69.70.84.xx - - [20/Jan/2010:15:54:06 -0600] "GET /framework2/ HTTP/1.1" 200 2968 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:08 -0600] "GET /foo/bar.js HTTP/1.1" 404 8642 "http://www.mydomain.com/framework2/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:10 -0600] "GET /framework2/ HTTP/1.1" 500 8511 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:11 -0600] "GET /framework/mydomain_ajax-dom.js HTTP/1.1" 304 - "http://www.mydomain.com/framework2/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:11 -0600] "GET /framework/templates/common/Reset.css HTTP/1.1" 304 - "http://www.mydomain.com/framework2/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:11 -0600] "GET /framework/templates/common/Error.css HTTP/1.1" 304 - "http://www.mydomain.com/framework2/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:11 -0600] "GET /framework/templates/common/Base.css HTTP/1.1" 304 - "http://www.mydomain.com/framework2/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
69.70.84.xx - - [20/Jan/2010:15:54:11 -0600] "GET /images/Error-48.png HTTP/1.1" 304 - "http://www.mydomain.com/framework2/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)"
'

  1. Line 1 is the document correctly served.
  2. Line 2 is the 404 of a dummy .js on my document.
  3. Line 3 is where the actual error occur (I hit refresh in my browser and my PHP issue the 304 header that crashes my script).
  4. Line 4, 5, 6, 7, 8 are caused by my custom error message (via .htaccess) that display the 500 error message.

Edit 2: I contacted Web host support to ask if I had access to more log they said no but on their side they have access to logs I can't see. My error was in there:

'backend: malformed header from script. Bad header=Not Modified: index.php'

So back to my question... Why on some hosts the first version don't work and on others it seems to be OK (even worse someone in the link above says that the second crashes while the first is OK)??

解决方案

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);

这篇关于PHP header()调用“崩溃”具有HTTP 500错误的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆