[SOLVED]HTTP Request 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

[SOLVED]HTTP Request headers.

Post 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.
Last edited by JellyFish on Mon Aug 11, 2008 4:38 pm, edited 1 time in total.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: HTTP Request headers.

Post by EverLearning »

The closest thing to what you want, that I know of, is $_SERVER superglobal. I don't know if that helps.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: HTTP Request headers.

Post by JellyFish »

I need this so I know which page the HTTP POST is from.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: HTTP Request headers.

Post 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']
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: HTTP Request headers.

Post by JellyFish »

Thanks Apollo, just what I needed!
Post Reply