2010-01-04 44 views
4

很簡單的問題:這兩個PHP(版本5+)頭部調用中的哪一個是最好的?在PHP中使用頭部

header('Not Modified', true, 304); 
header('HTTP/1.1 304 Not Modified'); 

我敢肯定,第一個是最價之一,但只是好奇,如果PHP將「修正」下HTTP 1.0,第二個如果...

謝謝!

編輯:其中一個頭在我的Web主機上崩潰PHP。後續問題在: PHP header() call "crashing" script with HTTP 500 error

回答

8

我會使用這一個:

header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified', true, 304); 

$_SERVER['SERVER_PROTOCOL']包含像HTTP/1.0HTTP/1.1請求中使用的協議。


編輯我不得不承認,我的建議是毫無意義的。經過幾次測試後,我注意到如果第一個參數是有效的HTTP status line,PHP將使用該狀態行,而不管第三個參數是否給出了第二個狀態碼。第二個參數(文件名替代)也沒用,因爲不能有多個狀態行。

所以在這個調用中的第二和第三個參數只是多餘的:

header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified', true, 304); 

只需使用這個來代替:

header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified'); 
2

我會去第二個,因爲HTTP響應代碼參數只支持> = PHP 4.3.0(這可能會影響代碼的可移植性)。

我已經做了很多次,沒有遇到任何不支持HTTP/1.1的客戶端,所以除非你有特殊情況,否則我不應該認爲這會是一個問題。

+2

爲什麼你關心php4的兼容性?你不使用對象(或以非常有限的方式使用它們)? – 2010-01-04 15:15:25

+2

確實,我認爲PHP 4冷死了。我不再支持我的任何腳本中的PHP <5。 – AlexV 2010-01-04 15:16:41

+0

相當不錯,但仍然有大量的PHP 4服務器,所以如果需要代碼可移植,那麼這是一個考慮因素。 爲什麼你仍然在意HTTP/1.0? – 2010-01-04 15:19:09

4

大約有第一頭調用,值得指出的行爲,兩件事情:

  • 如果您提供的第三個參數,PHP會忽略第一個字符串參數發送給定的正確的響應數。這可能會使第一種方法更容易出現程序員錯誤。
  • PHP似乎與一個HTTP即使請求與HTTP做出/ 1.1響應/ 1.0
+0

似乎第一點是不正確的(至少對於我的虛擬主機),因爲我得到了「後端:腳本格式不正確的標題」。錯誤標題=未修改:index.php「... – AlexV 2010-01-21 14:13:38

2

我通常會用第二個例子去應對 - 然而,在使用apachebench最近標杆的應用程序,我們注意到ab經常掛。

調試後,經鑑定,在這種風格的標題:

header('HTTP/1.1 304 Not Modified') 

是罪魁禍首(是的,我也沒辦法),並改變它後,

header('Not Modified', true, 304); 

信不信由你或者不開始工作。非常奇怪,但有些事情需要考慮。我可能會使用第二種方法。

+0

奇怪...我的主機崩潰時使用頭(500)錯誤('未修改',真,304);(我得到錯誤」後端:從腳本格式錯誤的頭。Bad header = Not Modified:index.php「)。 – AlexV 2010-01-21 14:14:59

1

我認爲秋葵的答案是最明智的爲止。然而試試這個:

<?php 
header('Gobbledy Gook', true, 304); 
?> 

如果第一個字符串是不正確的頭被丟棄。如果IY看起來像會被添加到一個標題有效的頭 - 試試這個:

<?php 
header('Cache-Control: max-age=10', true, 304); 
?> 

的頭手動(),並注意特殊情況 - 在一般我認爲它不建議依靠這種內置在啓發式。

但是,我猜你實際上對通過代理/瀏覽器緩存內容感興趣。在大多數情況下,延遲比帶寬更成爲一個問題。接下來考慮緩存內容陳舊時瀏覽器的行爲 - 在沒有更新的緩存信息的情況下,它會不斷向服務器發出重複請求,以查看內容是否仍舊陳舊。

I.e.在大多數情況下,忽略請求的條件部分(或者甚至更好地在Web服務器上剝離它們)實際上會提高性能。

+0

」如果第一個字符串不是合適的頭部,它將被丟棄。如果iy確實看起來像一個有效的頭文件,它會被追加到頭文件中「你確定嗎?當我使用頭文件('Not Modified',true,304)時,我得到錯誤」backend:來自腳本的格式不正確的頭文件。 Bad header = Not Modified:index.php「是因爲那個嗎? – AlexV 2010-01-21 14:18:28

2

以供將來參考http_response_code()函數在5.4來臨:

http://www.php.net/manual/en/function.http-response-code.php

另一種方法是:

if (!function_exists('http_response_code')) { 
    function http_response_code($code = NULL) { 

     if ($code !== NULL) { 

      switch ($code) { 
       case 100: $text = 'Continue'; break; 
       case 101: $text = 'Switching Protocols'; break; 
       case 200: $text = 'OK'; break; 
       case 201: $text = 'Created'; break; 
       case 202: $text = 'Accepted'; break; 
       case 203: $text = 'Non-Authoritative Information'; break; 
       case 204: $text = 'No Content'; break; 
       case 205: $text = 'Reset Content'; break; 
       case 206: $text = 'Partial Content'; break; 
       case 300: $text = 'Multiple Choices'; break; 
       case 301: $text = 'Moved Permanently'; break; 
       case 302: $text = 'Moved Temporarily'; break; 
       case 303: $text = 'See Other'; break; 
       case 304: $text = 'Not Modified'; break; 
       case 305: $text = 'Use Proxy'; break; 
       case 400: $text = 'Bad Request'; break; 
       case 401: $text = 'Unauthorized'; break; 
       case 402: $text = 'Payment Required'; break; 
       case 403: $text = 'Forbidden'; break; 
       case 404: $text = 'Not Found'; break; 
       case 405: $text = 'Method Not Allowed'; break; 
       case 406: $text = 'Not Acceptable'; break; 
       case 407: $text = 'Proxy Authentication Required'; break; 
       case 408: $text = 'Request Time-out'; break; 
       case 409: $text = 'Conflict'; break; 
       case 410: $text = 'Gone'; break; 
       case 411: $text = 'Length Required'; break; 
       case 412: $text = 'Precondition Failed'; break; 
       case 413: $text = 'Request Entity Too Large'; break; 
       case 414: $text = 'Request-URI Too Large'; break; 
       case 415: $text = 'Unsupported Media Type'; break; 
       case 500: $text = 'Internal Server Error'; break; 
       case 501: $text = 'Not Implemented'; break; 
       case 502: $text = 'Bad Gateway'; break; 
       case 503: $text = 'Service Unavailable'; break; 
       case 504: $text = 'Gateway Time-out'; break; 
       case 505: $text = 'HTTP Version not supported'; break; 
       default: 
        exit('Unknown http status code "' . htmlentities($code) . '"'); 
       break; 
      } 

      $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); 

      header($protocol . ' ' . $code . ' ' . $text); 

      $GLOBALS['http_response_code'] = $code; 

     } else { 

      $code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200); 

     } 

     return $code; 

    } 
} 

在這個例子中,我使用$ GLOBALS,但你可以使用無論你喜歡什麼樣的存儲機制......我不認爲有辦法返回當前狀態代碼:

https://bugs.php.net/bug.php?id=52555

供參考的錯誤代碼,我從PHP源代碼有:

http://lxr.php.net/opengrok/xref/PHP_5_4/sapi/cgi/cgi_main.c#354

而如何在當前的HTTP頭中發送,與變量,它使用:

http://lxr.php.net/opengrok/xref/PHP_5_4/main/SAPI.c#856

相關問題