I have a shutdown function that checks if only redirects have been redirected. From headers_list () I can get the sent headers and see the location header. My question is how can I determine which http_response_code was used in header () . The response list is missing from the list of headers.
Sample code to play. I do not use call forwarding in the sample code, otherwise it will be cyclic. Most importantly, I would like to detect 301 against any other kind of redirect. This will be inside drupal (via drupal_goto using hook_exit ); but the code example below shows the problem. I do not know what status number was passed to the browser through 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(' ', ' ', 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; } ?>
This code displays this
X-test: junk 1 Array ( [0] => X-Powered-By: PHP/5.2.10 [1] => X-test: junk 1 )
source share