Ray Ray - 2 months ago 17
Apache Configuration Question

In PHP < 5.4 is there any way to get a status response code you just set?

I know in php 5.4 there is a new function

http_response_code()
, but in prior versions how would you get a response code you just set?

Similar to Is there any way to get the current HTTP response code from PHP?

I have this need:

//sometime earlier
header('HTTP/1.1 404 Not Found');


//sometime later, detect if error was set
$status = some_magic_way_to_find_status();


Does anyone have a way in php 5.3 or below to do this?

SDC SDC
Answer

I don't think it's possible.

You could wrap the header() function:

function my_status_header($setHeader=null) {
    static $theHeader=null;
    //if we already set it, then return what we set before (can't set it twice anyway)
    if($theHeader) {return $theHeader;}
    $theHeader = $setHeader;
    header('HTTP/1.1 '.$setHeader);
    return $setHeader;
}

Or, of course, you could always upgrade to PHP5.4.

Comments