I am Peter Breuls. I write web applications in PHP, movie reviews and irregularly something on this weblog. Welcome!
Through my company Devize, I'm available as a developer or a consultant for websites or web applications.
I work as an Administrator at online community FOK! and as a Lead Developer at frontoffice supplier SIMgroep.

Fetching HTTP request headers in PHP

Tech / Web developmentcomment

Well, that was easy. I was looking for a way to fetch HTTP request headers in PHP, for the implementation of Conditional GET in an RSS module. PHP has this function, apache_request_headers(), which returns an array containing all the headers you could possibly need. Problem solved!

But; when the software you're working on (in my case a content management system) has to work on both Linux/Apache and Windows/IIS servers, there's a bit of a problem: Apache-functions won't work if there's no Apache to work with. Fortunately, you don't need functions to fetch specific headers: PHP has already gathered them for you.

When a client requests a PHP-driven page on your web server, you can check for request headers in the $_SERVER array. For instance, the If-Modified-Since header is stored in $_SERVER['HTTP_IF_MODIFIED_SINCE']. This will work for every header you can think of: PHP just converts them all to the right variable. A header This-One-Doesnt-Exist becomes $_SERVER['THIS_ONE_DOESNT_EXIST'] in PHP code. Just like that!