Page 1 of 1

Capturing PHP headers

Posted: Mon Oct 15, 2007 1:19 am
by shannah
Hello,

I am writing an output caching module, and I am stuck on the issue of preserving the content type of output. When the php script runs I may call
header('Content-Type: text/javascript');
to indicate that the output is javascript.
Similarly, I might call
header('Content-Type: text/xml');
to indication that the output is xml.

It is easy to use output buffering (ob_start etc ..) to capture the actual content that is being output so that I can cache it for later. However how do I capture the headers that have been output by my script? If I must, I'll create a wrapper for the header function that saves the headers so that I can access them - but this would lose the genericity of the module.

Any tips appreciated.

Best regards

Steve Hannah
http://fas.sfu.ca/dataface

Posted: Mon Oct 15, 2007 2:37 am
by Kieran Huggins
Check out http://ca.php.net/headers-list - I have a feeling it's exactly what you're looking for. (PHP 5 only)

Posted: Mon Oct 15, 2007 11:30 am
by shannah
Thanks Kieran. This is exactly what I was looking for.... too bad it's only PHP 5.. I wonder if there are any PHP 4 alternatives.

Posted: Mon Oct 15, 2007 11:47 am
by John Cartwright
http://ca.php.net/get_headers

The user comments have some php4 alternatives.

Posted: Mon Oct 15, 2007 12:15 pm
by shannah
Thanks jcart. This function appears to for getting the headers returned when you perform an HTTP request within your script. I'm looking for ways to obtain the headers that are sent from my current script. Kieran posted the headers_list function above which works for PHP 5. That man page doesn't show any PHP4 alternatives, however...

I use PHP 5 myself, but I like to find ways to maintain PHP 4 compatibility whereever possible for distribution purposes.

Best regards

Steve

Posted: Mon Oct 15, 2007 4:05 pm
by Kieran Huggins
Sorry Steve, looks like you'll have to drop support for php4 on this one.

I don't imagine there could be a workaround, since it's access to a feature in the Zend engine itself. On the other hand, I could be dead wrong.

Posted: Mon Oct 15, 2007 10:44 pm
by RobertGonzalez
Perhaps you could take a look at the PHP source and see if there isn't something that might be readable in there that could help? It is a long shot, but it could work.