Get http request headers in php

When a browser makes a request to a php script, the browser sends some http headers which can look like this :

Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: UTF-8,*;q=0.5
Cookie: username=admin; password=21232ffc3; PHPSESSID=o5m4e2td1m66c4pkjdag9vs0u2

The php script under request , may want to access these http headers. PHP has a method getallheaders() which provides these headers. So the code should be like this :

foreach (getallheaders() as $name => $value) 
{
    echo "$name: $valuen";
}

and the output should be like this :

Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: UTF-8,*;q=0.5
Cookie: username=admin; password=21232ffc3; PHPSESSID=o5m4e2td1m66c4pkjdag9vs0u2

However the method getallheaders is available only when PHP is running as a Apache Module which is the Apache2handler.
When PHP is being run via CGI , this method would be unavailable. Calling it would give a :

Fatal error: Call to undefined function getallheaders()

In this case a workaround can be used which is as follows :

/**
	The following function gets the http headers of a client request
	when php is running as CGI
*/
if(!function_exists('getallheaders'))
{
 function getallheaders() 
 {
  foreach($_SERVER as $name => $value)
  {
   if(substr($name, 0, 5) == 'HTTP_')
   {
    $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
   }
  }
  return $headers;
 }
}

The above method provides the http headers by fetching them from the $_SERVER variable, since they are present in the server variables in the form :

[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[HTTP_ACCEPT_CHARSET] => UTF-8,*;q=0.5
[HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
[HTTP_CACHE_CONTROL] => max-age=0
[HTTP_CONNECTION] => keep-alive
[HTTP_COOKIE] => __utmz=179618234.1309856897.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=179618234.703966342.1309856897.1309856897.1309856897.1
[HTTP_HOST] => www.yoursite.com
[HTTP_USER_AGENT] => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30






The documentation mentions that the method would be available in FastCGI from php 5.4

Another method called apache_request_headers() gives the same results :

foreach (apache_request_headers() as $name => $value) 
{
    echo "$name: $valuen";
}
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: UTF-8,*;q=0.5
Cookie: username=admin; password=21232f297a57a5801fc3; PHPSESSID=o5m4e2td1m66c4pkjdag9vs0u2

This method is also unavailable in CGI mode and is available in FastCGI from php 5.4

Last Updated On : 22nd November 2011

Subscribe to get updates delivered to your inbox

Leave a comment