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.

PHP:
  1. /**
  2. * Returns status code information.
  3. *
  4. * If not status code is passed to the method then it will return a list of
  5. * all status codes with their HTTP version and description.  If a status
  6. * code is supplied then it will return the string to be used with the
  7. * header() function.
  8. *
  9. * @param integer $status The status code
  10. * @return array|string
  11. * @access public
  12. * @todo Verify the correct HTTP versions
  13. */
  14. function status_code($status = null)
  15. {
  16.     $codes = array(
  17.         100 => array('HTTP/1.1', 'Continue'),
  18.         101 => array('HTTP/1.1', 'Switching Protocols'),
  19.         200 => array('HTTP/1.0', 'OK'),
  20.         201 => array('HTTP/1.0', 'Created'),
  21.         202 => array('HTTP/1.0', 'Accepted'),
  22.         203 => array('HTTP/1.0', 'Non-Authoritative Information'),
  23.         204 => array('HTTP/1.0', 'No Content'),
  24.         205 => array('HTTP/1.0', 'Reset Content'),
  25.         206 => array('HTTP/1.0', 'Partial Content'),
  26.         300 => array('HTTP/1.0', 'Multiple Choices'),
  27.         301 => array('HTTP/1.0', 'Permanently at another address - consider updating link'),
  28.         302 => array('HTTP/1.1', 'Found at new location - consider updating link'),
  29.         303 => array('HTTP/1.1', 'See Other'),
  30.         304 => array('HTTP/1.0', 'Not Modified'),
  31.         305 => array('HTTP/1.0', 'Use Proxy'),
  32.         306 => array('HTTP/1.0', 'Switch Proxy'), // No longer used, but reserved
  33.         307 => array('HTTP/1.0', 'Temporary Redirect'),
  34.         400 => array('HTTP/1.0', 'Bad Request'),
  35.         401 => array('HTTP/1.0', 'Authorization Required'),
  36.         402 => array('HTTP/1.0', 'Payment Required'),
  37.         403 => array('HTTP/1.0', 'Forbidden'),
  38.         404 => array('HTTP/1.0', 'Not Found'),
  39.         405 => array('HTTP/1.0', 'Method Not Allowed'),
  40.         406 => array('HTTP/1.0', 'Not Acceptable'),
  41.         407 => array('HTTP/1.0', 'Proxy Authentication Required'),
  42.         408 => array('HTTP/1.0', 'Request Timeout'),
  43.         409 => array('HTTP/1.0', 'Conflict'),
  44.         410 => array('HTTP/1.0', 'Gone'),
  45.         411 => array('HTTP/1.0', 'Length Required'),
  46.         412 => array('HTTP/1.0', 'Precondition Failed'),
  47.         413 => array('HTTP/1.0', 'Request Entity Too Large'),
  48.         414 => array('HTTP/1.0', 'Request-URI Too Long'),
  49.         415 => array('HTTP/1.0', 'Unsupported Media Type'),
  50.         416 => array('HTTP/1.0', 'Requested Range Not Satisfiable'),
  51.         417 => array('HTTP/1.0', 'Expectation Failed'),
  52.         449 => array('HTTP/1.0', 'Retry With'), // Microsoft extension
  53.         500 => array('HTTP/1.0', 'Internal Server Error'),
  54.         501 => array('HTTP/1.0', 'Not Implemented'),
  55.         502 => array('HTTP/1.0', 'Bad Gateway'),
  56.         503 => array('HTTP/1.0', 'Service Unavailable'),
  57.         504 => array('HTTP/1.0', 'Gateway Timeout'),
  58.         505 => array('HTTP/1.0', 'HTTP Version Not Supported'),
  59.         509 => array('HTTP/1.0', 'Bandwidth Limit Exceeded') // not an official HTTP status code
  60.     );
  61.  
  62.     if ($status === null || !isset($codes[$status])) {
  63.         return $codes;
  64.     }
  65.  
  66.     return "{$codes[$status][0]} $status {$codes[$status][1]}";
  67. }

With all the status codes in a function you can easily return them all, or if you use something like this:

PHP:
  1. /**
  2. * Send a standard HTTP status header and exit.
  3. *
  4. * @param integer $status What status header number to send
  5. * @param boolean $exit Whether to exit the script or not
  6. * @access public
  7. */
  8. function h($status, $exit = true)
  9. {
  10.     $header = status_code($status);
  11.     if (!is_array($header)) {
  12.         header($header);
  13.         if ($exit) {
  14.             exit;
  15.         }
  16.     }
  17. }

then it's super easy to set a HTTP status code. Simply do:

PHP:
  1. h(403);

0 Responses to “HTTP status codes”


  1. No Comments

Leave a Reply

You must login to post a comment.




Mp3 sparks Allofmp3