Capturing PHP headers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shannah
Forum Newbie
Posts: 13
Joined: Mon Oct 15, 2007 1:01 am

Capturing PHP headers

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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)
shannah
Forum Newbie
Posts: 13
Joined: Mon Oct 15, 2007 1:01 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://ca.php.net/get_headers

The user comments have some php4 alternatives.
shannah
Forum Newbie
Posts: 13
Joined: Mon Oct 15, 2007 1:01 am

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply