PHP: Get the http status code that the script belongs to, just sent via the shutdown function

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(' ', '&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; } ?> 

This code displays this

 X-test: junk 1 Array ( [0] => X-Powered-By: PHP/5.2.10 [1] => X-test: junk 1 ) 
+4
source share
1 answer

Version 302033 added the http_response_code function in response to just the question you are describing , but I'm not sure when it will be included in the release. This is not in 5.3.4. If you have access, you can create a fixed version of PHP with the added function. If not, you can ask him who has access to your host.

+3
source

Source: https://habr.com/ru/post/1332849/


All Articles