php : Show the headers received from the browser
Question by roopunk
So when a browser makes a HTTP request to a server, it is in the form some headers (get/post, cookies, host, User Agent, etc..). Is there a way I can read and display them in a php script? And yes $_GET, $_POST and $_COOKIE are there alright. I was looking for the rest of header info. eg http://pgl.yoyo.org/http/browser-headers.php Thanks.
Answer by Vito Nordloh
My favorite: http://php.net/manual/en/function.apache-request-headers.php
Fetches all HTTP request headers from the current request.
<?php
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />n";
}
?>
Answer by Starx
get_headers() function is what you are looking for. As quoted
get_headers — Fetches all the headers sent by the server in response
to a HTTP request
Simple example:
$url = 'http://www.example.com';
print_r(get_headers($url));
Outputs all the information the send by the server:
Array (
[0] => HTTP/1.1 200 OK [1] => Date: Sat, 29 May 2004 12:28:13 GMT [2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT [4] => ETag: "3f80f-1b6-3e1cb03b" [5] => Accept-Ranges: bytes [6] => Content-Length: 438 [7] => Connection: close [8] => Content-Type: text/html )
Update
TO receive the information that is send by browsers, they can be accessed from $_SERVER
super global variable. For example, the following snippet gives all the browser related information.
echo $_SERVER['HTTP_USER_AGENT'];
Similarly,
REQUEST_METHOD
: gives the HTTP Request method such as GET, HEAD, Put, PostHTTP_HOST
: gives the HOST informationHTTP_COOKIE
: gives the raw information about the Cookie header [Source]