我只是做了一個快速的測試用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