HTTP status codes
Quite often I find myself having to send a HTTP status code in the PHP I’m writing. This typically comes down to usually a ‘Forbidden’, ‘No Content’ or a redirect. The trouble is, I usually can’t remember the exact code of text used for the slightly more obscure codes, or at least the ones I don’t use often… Well, even for the ones I use often, if truth be told!
So I put them all in the following function which you may find useful, too.
/** * Returns status code information. * * If not status code is passed to the method then it will return a list of * all status codes with their HTTP version and description. If a status * code is supplied then it will return the string to be used with the * header() function. * * @param integer $status The status code * @return array|string * @access public * @todo Verify the correct HTTP versions */ function status_code($status = null) { $codes = array( 100 => array('HTTP/1.1', 'Continue'), 101 => array('HTTP/1.1', 'Switching Protocols'), 200 => array('HTTP/1.0', 'OK'), 201 => array('HTTP/1.0', 'Created'), 202 => array('HTTP/1.0', 'Accepted'), 203 => array('HTTP/1.0', 'Non-Authoritative Information'), 204 => array('HTTP/1.0', 'No Content'), 205 => array('HTTP/1.0', 'Reset Content'), 206 => array('HTTP/1.0', 'Partial Content'), 300 => array('HTTP/1.0', 'Multiple Choices'), 301 => array('HTTP/1.0', 'Permanently at another address - consider updating link'), 302 => array('HTTP/1.1', 'Found at new location - consider updating link'), 303 => array('HTTP/1.1', 'See Other'), 304 => array('HTTP/1.0', 'Not Modified'), 305 => array('HTTP/1.0', 'Use Proxy'), 306 => array('HTTP/1.0', 'Switch Proxy'), // No longer used, but reserved 307 => array('HTTP/1.0', 'Temporary Redirect'), 400 => array('HTTP/1.0', 'Bad Request'), 401 => array('HTTP/1.0', 'Authorization Required'), 402 => array('HTTP/1.0', 'Payment Required'), 403 => array('HTTP/1.0', 'Forbidden'), 404 => array('HTTP/1.0', 'Not Found'), 405 => array('HTTP/1.0', 'Method Not Allowed'), 406 => array('HTTP/1.0', 'Not Acceptable'), 407 => array('HTTP/1.0', 'Proxy Authentication Required'), 408 => array('HTTP/1.0', 'Request Timeout'), 409 => array('HTTP/1.0', 'Conflict'), 410 => array('HTTP/1.0', 'Gone'), 411 => array('HTTP/1.0', 'Length Required'), 412 => array('HTTP/1.0', 'Precondition Failed'), 413 => array('HTTP/1.0', 'Request Entity Too Large'), 414 => array('HTTP/1.0', 'Request-URI Too Long'), 415 => array('HTTP/1.0', 'Unsupported Media Type'), 416 => array('HTTP/1.0', 'Requested Range Not Satisfiable'), 417 => array('HTTP/1.0', 'Expectation Failed'), 449 => array('HTTP/1.0', 'Retry With'), // Microsoft extension 500 => array('HTTP/1.0', 'Internal Server Error'), 501 => array('HTTP/1.0', 'Not Implemented'), 502 => array('HTTP/1.0', 'Bad Gateway'), 503 => array('HTTP/1.0', 'Service Unavailable'), 504 => array('HTTP/1.0', 'Gateway Timeout'), 505 => array('HTTP/1.0', 'HTTP Version Not Supported'), 509 => array('HTTP/1.0', 'Bandwidth Limit Exceeded') // not an official HTTP status code ); if ($status === null || !isset($codes[$status])) { return $codes; } return "{$codes[$status][0]} $status {$codes[$status][1]}"; }
With all the status codes in a function you can easily return them all, or if you use something like this:
/** * Send a standard HTTP status header and exit. * * @param integer $status What status header number to send * @param boolean $exit Whether to exit the script or not * @access public */ function h($status, $exit = true) { $header = status_code($status); if (!is_array($header)) { header($header); if ($exit) { exit; } } }
then it’s super easy to set a HTTP status code. Simply do:
h(403);