3
votes

I am looking to put together a website that displays the full HTTP Request Headers and HTTP Response Headers for the loading of the page itself. For instance. If someone browses to http://example.com/index.php , I want the following to display:

HTTP Request
GET /index.php HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16
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: 115
Connection: keep-alive

HTTP Response
HTTP/1.1 200 OK
Date: Mon, 21 Dec 2011 10:20:46 GMT
Server: Apache/2.2.15 (Red Hat)
X-Powered-By: PHP/5.3.3
Content-Length: 1169
Connection: close
Content-Type: text/html; charset=UTF-8

We were able to get the Request header to display fairly simply using the following PHP code:

print $_SERVER['REQUEST_METHOD']." ".$_SERVER['REQUEST_URI']." ".$_SERVER['SERVER_PROTOCOL']."<br>";
foreach (apache_request_headers() as $name => $value)
        echo "$name: $value<br>";

But are having some difficulties with the HTTP Response header. Anyone have any ideas of how we can do this? It does not have to be PHP if you have a method that works in Perle or CGI or whatever.

To be clear, I don't mean to set the HTTP Response to anything specific, only display the response served by the web server to load the page.

4
Ahh, I had the end goal of the page in the original comment but it errored out when I first tried posting, forgot to re-include it. The end goal of the page is to sit on webservers behind load balancers so we can see/test/train on how a load balancer affects HTTP strreams. - Eddie
Can you use Javascript to display this information, it will be much easier for you. - Gabriel Gartz
You obviously can not access the response headers from PHP because they will be added after your script is finished. Even proxy servers can add their own header lines to the response without the knowledge of your server. This can only be done at the client side. - vbence

4 Answers

3
votes

You want to use headers_list()

http://www.php.net/manual/en/function.headers-list.php

headers_list() will return a list of headers to be sent to the browser / client. To determine whether or not these headers have been sent yet, use headers_sent().

2
votes

Well here is the issue, the response header is generated after the PHP (or any server-side language for that matter) has already completed its job.

To put it in english its like the post man handing you a letter and you asking him to explain how the process of handing you the letter went. He will probably just look at you dumb.

You will need a client-side language (ie. JavaScript) to perform this task.

1
votes

Use PHP to get the headers sent to the web Server.

http://www.php.net/manual/en/function.apache-request-headers.php

Use JavaScript to get headers sent by the web server. I would suggest using jQuery for that.

http://api.jquery.com/jQuery.ajax/#jqXHR

This way you are sure that you get all the headers which are either received by the web server or the browser.