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

Kordian Bruck

I'm a TUM Computer Science Alumni. Pizza enthusiast. Passionate for SRE, beautiful Code and Club Mate. Currently working as an SRE at Google. Opinions and statements in this blog post are my own.

Leave a Reply