PHPs http_response_code – 5.3 compatibility snippet

The Api of PHP just rocks, easy to use for the things you need. Sometime compatibility is an issue though. Many webhosters didn’t upgrade yet from 5.3 to anything reasonable, so I might as well keep this snippet here for future reference:

if (!function_exists('http_response_code')) {
    function http_response_code($newcode = NULL) {
        static $code = 200;
        if ($newcode !== NULL) {
            header('X-PHP-Response-Code: ' . $newcode, true, $newcode);
            if (!headers_sent()) {
                $code = $newcode;
            }
        }
        return $code;
    }
}

 

Published by

KB

I'm a student at TUM in Computer Science & Pizza eating. Passionate for SRE, beautiful Code and Club Mate.