Rectangle 27 0

PHP http_response_code(); versus header();?

In summary - The differences between http_response_code and header for setting response codes:

If http_response_code(403); is used somewhere in a script, should it be followed by exit? Or does further execution get terminated implicitly by http_response_code(403);?

It doesn't terminate execution. So if you want to exit after setting the response code you'll have to explicitly exit.

Note that this means your response code must match a response code that PHP knows about. You can't create your own response codes using this method, however you can using the header method. Note also that http_response_code is only available in PHP 5.4.0 and higher.

The two methods are essentially functionally equivalent. http_response_code is basically a shorthand way of writing a http status header, with the added bonus that PHP will work out a suitable Reason Phrase to provide by matching your response code to one of the values in an enumeration it maintains within php-src/main/http_status_codes.h.

Rectangle 27 0

PHP http_response_code(); versus header();?

 * Sets the response code and reason
 * @param int    $code
 * @param string $reason
function setResponseCode($code, $reason = null) {
    $code = intval($code);

    if (version_compare(phpversion(), '5.4', '>') && is_null($reason))
        header(trim("HTTP/1.0 $code $reason"));

setResponseCode(401,'Get back to the shadow');

Nice little wrapper method! In production, that else should already grab from a list of $reason values either hard-coded or from a language file with constants.

you can use it as: