2010-12-21 35 views
4

我有一個shutdown function,它檢查是否剛剛發出重定向。從headers_list()我可以得到標題發送並查看位置標題。我的問題是如何找出header()函數中使用的http_response_code。標題列表沒有響應代碼。PHP:獲取http狀態碼,通過關機功能發送自己的腳本

示例代碼來玩弄。我不在示例代碼中使用重定向,否則它會循環。主要的是我想檢測301與任何其他類型的重定向。這將在drupal內部(通過drupal_goto使用hook_exit);但下面的示例代碼顯示了該問題。我無法知道通過header()傳遞給瀏覽器的狀態號碼。

<?php 
register_shutdown_function('test'); 

if (mt_rand(0, 1)) { 
    header('X-test: junk 1', TRUE, 201); 
} 
else { 
    header('X-test: junk 0', TRUE, 202); 
} 

exit(); 


function test() { 
    if ($location = test_headers_contain('X-test: ')) { 
    // Would like to check the status code that was sent out 
    echo $location . '<br>'; 
    $list = headers_list(); 
    $txt = str_replace(' ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($list, TRUE)))); 
    echo $txt; 
    } 
} 

function test_headers_contain($text) { 
    if (function_exists('headers_list')) { 
    $list = headers_list(); 
    if (empty($list)) { 
     return FALSE; 
    } 
    foreach ($list as $header) { 
     $info = stristr($header, $text); 
     if ($info !== FALSE) { 
     return $info; 
     } 
    } 
    } 
    return FALSE; 
} 
?> 

此代碼將這個

X-test: junk 1 
Array 
(
    [0] => X-Powered-By: PHP/5.2.10 
    [1] => X-test: junk 1 
) 

回答

3

Revision 302033響應只是那種issue you describe添加的功能http_response_code,但我不能確定什麼時候會被包含在一份新聞稿中。它不在5.3.4中。如果您有權訪問,則可以添加此功能來構建補丁的PHP版本。如果沒有,您可以向主持人有權訪問的任何人請求。

+0

謝謝;不是我所希望的答案......但這就是它的方式。至少新功能在php的SVN中。代碼將用於升壓模塊;現在我希望Drupal會接受我的補丁drupal_goto。 http://drupal.org/node/1003838 – mikeytown2 2010-12-21 02:28:21