Page 1 of 1

[SOLVED]HTTP Request headers.

Posted: Sat Aug 09, 2008 3:39 pm
by JellyFish
Hello!

Is there an way to get and turn the HTTP request headers into an associative array, using php? For example, let's say someone makes an HTTP request to somepage.php. The HTTP request looks like so:

Code: Select all

POST somepage.php HTTP/1.1
Host: url.com
From: somewhere.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 12345
 
body and stuff
Can somepage.php somehow read all the request headers of that HTTP request and turn them into an array? So that I could write something like:

Code: Select all

if ($headers['From'] == "somewhere.net" && $headers['From'] != "somewhere.com") {  }
Thanks for reading.

Re: HTTP Request headers.

Posted: Sat Aug 09, 2008 6:39 pm
by EverLearning
The closest thing to what you want, that I know of, is $_SERVER superglobal. I don't know if that helps.

Re: HTTP Request headers.

Posted: Sun Aug 10, 2008 11:51 am
by JellyFish
I need this so I know which page the HTTP POST is from.

Re: HTTP Request headers.

Posted: Sun Aug 10, 2008 12:46 pm
by Apollo
The built-in function apache_response_headers will probably do what you want.

Note that the HTTP request string is not included in the array returned by this function, but you can obtain this with the $_SERVER array:

Code: Select all

$HttpRequest = $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL'];
If you need to know what page you came from, use $_SERVER['HTTP_REFERER']

Re: HTTP Request headers.

Posted: Mon Aug 11, 2008 4:37 pm
by JellyFish
Thanks Apollo, just what I needed!