Apache, PHP: Get Client Browser HTTP Request Headers Information

by Yang Yang on June 2, 2009

Every HTTP requests made by any client web browsers to an Apache should conform to the HTTP specification and provide certain set of headers information for the server to parse and understand. Useful headers information that can be retrieved in PHP by function apache_request_headers() includes:

  1. User-Agent: Operating System, browser and its version number, …
  2. Accept-Language: Requesting client language
  3. Accept-Charset: Character set of the client

To get an array of the above client request headers information, just use apache_request_headers() function:

$headers = apache_request_headers();

And you’ll get:

Array
(
    [Host] => localhost
    [User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5
    [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [Accept-Language] => en-us,en;q=0.5
    [Accept-Encoding] => gzip,deflate
    [Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
    [Keep-Alive] => 300
    [Connection] => keep-alive
    [Cache-Control] => max-age=0
)

Similarly, you can use apache_response_headers() to get the HTTP response headers information sent from your server to the client.

Jon May 4, 2010 at 2:44 pm

Nice and simple function they have for this purpose. Thanks for the tip!

Comments on this entry are closed.

Previous post:

Next post: