Retrieve HTTP Headers


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4. * Access the HTTP Request
  5. */
  6. class http_request extends bantr {
  7.  
  8. /** additional HTTP headers not prefixed with HTTP_ in $_SERVER superglobal */
  9. var $add_headers = array('CONTENT_TYPE', 'CONTENT_LENGTH');
  10.  
  11. /**
  12. * Construtor
  13. * Retrieve HTTP Body
  14. * @param Array Additional Headers to retrieve
  15. */
  16. function http_request($add_headers = false) {
  17.  
  18. $this->retrieve_headers($add_headers);
  19. $this->body = @file_get_contents('php://input');
  20. }
  21.  
  22. /**
  23. * Retrieve the HTTP request headers from the $_SERVER superglobal
  24. * @param Array Additional Headers to retrieve
  25. */
  26. function retrieve_headers($add_headers = false) {
  27.  
  28. if ($add_headers) {
  29. $this->add_headers = array_merge($this->add_headers, $add_headers);
  30. }
  31.  
  32. if (isset($_SERVER['HTTP_METHOD'])) {
  33. $this->method = $_SERVER['HTTP_METHOD'];
  34. unset($_SERVER['HTTP_METHOD']);
  35. } else {
  36. $this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
  37. }
  38. $this->protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : false;
  39. $this->request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
  40.  
  41. $this->headers = array();
  42. foreach($_SERVER as $i=>$val) {
  43. if (strpos($i, 'HTTP_') === 0 || in_array($i, $this->add_headers)) {
  44. $name = str_replace(array('HTTP_', '_'), array('', '-'), $i);
  45. $this->headers[$name] = $val;
  46. }
  47. }
  48. }
  49.  
  50. /**
  51. * Retrieve HTTP Method
  52. */
  53. function method() {
  54. return $this->method;
  55. }
  56.  
  57. /**
  58. * Retrieve HTTP Body
  59. */
  60. function body() {
  61. return $this->body;
  62. }
  63.  
  64. /**
  65. * Retrieve an HTTP Header
  66. * @param string Case-Insensitive HTTP Header Name (eg: "User-Agent")
  67. */
  68. function header($name) {
  69. $name = strtoupper($name);
  70. return isset($this->headers[$name]) ? $this->headers[$name] : false;
  71. }
  72.  
  73. /**
  74. * Retrieve all HTTP Headers
  75. * @return array HTTP Headers
  76. */
  77. function headers() {
  78. return $this->headers;
  79. }
  80.  
  81. /**
  82. * Return Raw HTTP Request (note: This is incomplete)
  83. * @param bool ReBuild the Raw HTTP Request
  84. */
  85. function raw($refresh = false) {
  86.  
  87. if (isset($this->raw) && !$refresh) {
  88. return $this->raw; // return cached
  89. }
  90.  
  91. $headers = $this->headers();
  92. $this->raw = "{$this->method}
  93. ";
  94.  
  95. foreach($headers as $i=>$header) {
  96. $this->raw .= "$i: $header
  97. ";
  98. }
  99.  
  100. $this->raw .= "
  101. {$http_request->body}";
  102.  
  103. return $this->raw;
  104. }
  105.  
  106. }
  107.  
  108. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.