21st July, 2008

Parsing the HTTP request headers using PHP.   

Posted in programming | by eitama |

Whether you are building your website, or an intensive test environment,
getting things done with PHP is very easy, if you know what you are doing 8)

Let’s say you want to parse the request headers, and if a specific header is detected
act upon it’s detection? (Deny service, Send specific data and more…)
In order to parse the HTTP headers that are sent by the HTTP client to the server,
the following must be done :

1. Walk through all headers, one by one, in PHP the headers are located in $_SERVER array by default.
2. For each element in the array, check if it matches what we are looking for,
in our example it is the header “TID: 32423″ so we are looking for “HTTP_TID”
3. When matched, invoke some commands.

Example :

|   // Define function.
|   function getHeaders()
|   {
|       foreach ($_SERVER as $k => $v)
|      {
|           // We are going to search for a header looking like “TID: 12441″ in the request.
|          if (substr($k, 0, 8 ) == “HTTP_TID”)
|          {
|               // When such a header is matched, Send it back to the client.
|               header(”TID: $v”);
|           }
|       }
|   }
|
|   // Invoke the function upon request.
|   getHeaders();

Keep In Mind :
The above script will also match any header that starts with “TID”,
to enforce further checks you will have to work a little.

Yours truly,
Eitam



Leave a Reply

Top »
"If you can't join them, beat them!"
Search Evilbitz: