我只是做了一个快速的测试用PHP文件,temp.php
,其中包含的代码,这部分:
<?php
echo "Hello, World!\n";
die;
发送一个HTTP GET请求,该文件得到我的页面的内容:
$ telnet localhost 80
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /temp/temp.php HTTP/1.1
Host: localhost
HTTP/1.1 200 OK
Date: Thu, 08 Apr 2010 20:17:35 GMT
Server: Apache/2.2.12 (Ubuntu)
X-Powered-By: PHP/5.3.2RC2
Vary: Accept-Encoding
Content-Length: 14
Content-Type: text/html
Hello, World!
虽然发送HTTP HEAD请求并不:
$ telnet localhost 80
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HEAD /temp/temp.php HTTP/1.1
Host: localhost
HTTP/1.1 200 OK
Date: Thu, 08 Apr 2010 20:17:50 GMT
Server: Apache/2.2.12 (Ubuntu)
X-Powered-By: PHP/5.3.2RC2
Vary: Accept-Encoding
Content-Type: text/html
不知道这是总是正确的,但...
我记得有一个情况(前一段时间;是PHP 5.1)其中我必须在PHP代码中测试自己,如果我得到GET或HEAD请求。
编辑:一个其它附加测试
后,我只是做了另外一个测试:我temp.php
文件现在包含此:
<?php
file_put_contents('/tmp/a.txt', $_SERVER['REQUEST_METHOD'], FILE_APPEND);
var_dump($_SERVER['REQUEST_METHOD']);
die;
发送一个HTTP HEAD请求,我得到这个:
$ telnet localhost 80
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HEAD /temp/temp.php HTTP/1.1
Host: localhost
HTTP/1.1 200 OK
Date: Thu, 08 Apr 2010 20:21:30 GMT
Server: Apache/2.2.12 (Ubuntu)
X-Powered-By: PHP/5.3.2RC2
Vary: Accept-Encoding
Content-Type: text/html
Connection closed by foreign host.
这里没有输出。
,但看/tmp/a.txt文件:
$ cat /tmp/a.txt
HEAD
所以说:没有服务器发送的输出并不意味着有没有这样做;-)
是的,如果你把头('Foo:'。$ _ GET ['header']); error_log中( '试验');在脚本的顶部它将返回该标题/值和日志。如果您将该代码放入HTML主体的输出缓冲区中,则无法运行。所以PHP似乎不仅不返回消息体,甚至不会处理它。我会继续退出;毕竟我的头()调用。谢谢 – rkulla 2010-04-08 22:27:07
很高兴我能帮到你。 – webbiedave 2010-04-08 22:40:17
@rkulla底层PHP代码总是以与GET请求相同的方式执行。如果不是,请检查您的错误日志。 – Phil 2016-10-04 20:16:01