Forum Moderators: coopster

Message Too Old, No Replies

How to capture HTTP Response headers of the same page using PHP

HTTP Response headers PHP

         

santoshnagarajan

4:31 am on Feb 17, 2009 (gmt 0)

10+ Year Member



Hi All,

I have a requirement of displaying HTTP Response headers in the same page being redered using PHP.
This is to track various responses sent by the Apache Server for same page on different scenarios (like 200, 304, 503)

I have a single php file as follows:

-------index.php------------------------------------------------------------------
<?php
header('Cache-Control: no-cache');
print('<?xml version="1.0" encoding="ISO-8859-1"?>'."\n");
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head><title>Test</title>
<link type="text/css" rel="stylesheet" href="mycss.css"/>
</head>

<body>
<p>This page contains the header 'Cache-Control: no-cache'<br/>
<?php echo date("F j, Y, g:i:s a"); ?><br/>

</body>
</html>
-----end of index.php--------------------------------------------------------------------

This page has a header "Cache-Control". If this page is retrieved from the local Cache then 304 may be received. If Origin server sends then may be 200, if Origin server is unable to render then may be 503 received. Whereas it is required to capture these response headers in the same page and display.

What code can be added to include the Status code and Status text to get displayed. I have Apache/v1.3.27 on Linux with PHP 4.4.8

-Santosh

penders

10:01 am on Feb 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could have a look at the PHP functions apache_response_headers() [uk.php.net] and headers-list() [uk.php.net] (if PHP5) ....?

coopster

12:40 pm on Feb 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If this is for development troubleshooting you should also have a look at the Firefox browser extension, LiveHttpHeaders.

santoshnagarajan

2:54 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Dear coopster,
Thanks for your reply. However this is not for development troubleshooting. This is to test Browser capabilities. It is required to do from Server perspective and to know how Browser behaved on certain conditions.

Regards
Santosh

rocknbil

4:09 pm on Feb 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree w. penders, the tools at your disposal to capture response headers from the server are the various header..() functions.

santoshnagarajan

4:54 am on Feb 18, 2009 (gmt 0)

10+ Year Member



Dear Rocknbil,

At my end, I do not have control over upgrading Apache or PHP. The server is in public domain and our clients are using round the clock. I am forced to do with existing versions. Possibly I may have to wait for some more R&D/suggestions. However please keep posting your invaluable suggestions.

Santosh

coopster

2:44 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Version isn't the issue, unless you are trying to say that you are not running PHP as an Apache module? Have a look at the links that penders posted. Follow them back to the PHP Apache manual page to see if these functions are available to you.

santoshnagarajan

10:46 am on Feb 20, 2009 (gmt 0)

10+ Year Member



Dear coopster,

The PHP what I have supports function "apache_response_headers". If anyone can give me appropriate syntax or example to implement would be of great help. I tried as given in php.net but could not succeed.

Santosh

coopster

6:34 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<pre> 
<?php
header('Cache-Control: no-cache');
print 'Apache Headers List (headers going to be sent): ';
print_r(headers_list()) . "\n";
print 'Apache Response Headers: ';
print_r(apache_response_headers()) . "\n";
?>
</pre>
// Prints:
Apache Headers List (headers going to be sent): Array
(
[0] => X-Powered-By: PHP/5.2.8
[1] => Cache-Control: no-cache
)
Apache Response Headers: Array
(
[X-Powered-By] => PHP/5.2.8
[Cache-Control] => no-cache
)

santoshnagarajan

3:36 pm on Mar 4, 2009 (gmt 0)

10+ Year Member



Dear Coopster
Thsnks for timely help. However I am in the process of implementing it. Will inform once done with it.

Santosh