Important alert: (current site time 11/25/2014 6:48:55 PM EDT)
 

article

Using $HTTP_POST_VARS and $HTTP_GET_VARS

Email
Submitted on: 12/26/2001 4:18:14 PM
By: bleh 
Level: Beginner
User Rating: By 12 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 246899
 
     This describes how to use while(), list() and each() in conjunction with the predefined PHP variables $HTTP_POST_VARS and $HTTP_GET_VARS to read information passed to a page without knowing the variable names, or how many variables were passed.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Looping Through $HTTP_GET_VARS and $HTTP_POST_VARS

Quick Associative Array Overview

First off, to understand how to use these variables, you must understand what an associative array is. If you already know, then you can skip this part.

An associative array is simply an array who's key is not a number (i.e. $myArray[0], $myArray[1], etc.) but a word, as it were. For instance, if I have an associative array named "$myAssoc" who's values are the persons last name, and the keys are their first names, then it would look like this.

$myAssoc["charles"] = "chadwick";
where "charles" is the key (like 0, 1, etc) and "chadwick" is the value.

If you want a better explanation of this, I suggest reading the associative array information in the PHP manual found at www.php.net.

Reading Variables Passed Through GET and POST

For this example, I am going to use a standard while() loop to help us traverse through the associative array and read both it's keys and values. This will, as I am sure you are aware, execute until such time as there are no more keys to be read. So our first bit of code will use not only the while() statement, but two others, list() and each(). List() is used to assign a list of variables specified values in one operation. The each() function will pull a key and a value out of an array, and then advance the pointer to the next element in the array. So the first part of the code will look like so.

while(list($key, $value) = each($HTTP_GET_VARS))

This code is basically using the list function to assign information to our $key and $value variables, which is coming from the each() function. Then all we need to do is echo this information out to our page.

while(list($key, $value) = each($HTTP_GET_VARS))
{
echo "$key = $value(br)";
}

NOTE: At the end of my echo statement, I have put parens () around my HTML line break because it won't display on this page otherwise. If you are using this code on a site, make sure to replace the parens with actual HTML opening and closing brackets. Also note that both the equal sign and the HTML line break are NOT necassary in this code.

Let's say that our url is this:

www.myhomepage.com/test.php?var1=one&var2=two&var3=three&var4=four

This is the GET method. Our variables are "var1", "var2", "var3", and "var4". Their respective values in this example are "one", "two", "three", and "four". If we pass this along to a page that has our code on it, then the output would be like so:

var1 = one
var2 = two
var3 = three
var4 = four

If we are using the POST method, with the same variables, we would have the same output. However, you would need to use the $HTTP_POST_VARS variable.

NOTE: If you are using these, or any other predefined PHP variables in a function, you must declare them as global inside the function or this won't work.

That's it. This is pretty much beginner information, but I went for a while without realizing that this could be done, and it caused me to spend a lot more time coding than need be.


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/6/2002 9:55:07 PMDustin

Great little tutorial. It was exactly what I was looking for. Thanks!
(If this comment was disrespectful, please report it.)

 
9/20/2002 8:40:56 PMDarryl Porter

The writing leves to many bases un-covered. It asumes that the beginner know a lot about PHP
(If this comment was disrespectful, please report it.)

 
3/21/2003 9:55:29 AM

Can you tell me how to send file to server ?
(If this comment was disrespectful, please report it.)

 
8/21/2003 9:09:17 AM

In your statement:
www.myhomepage.com/test.php?var1=one&var2=two&var3=three&var4=four

This is the GET method. Our variables are "var1", "var2", "var3", and "var4". Their respective values in this example are "one", "two", "three", and "four". If we pass this along to a page that has our code on it, then the output would be like so:

var1 = one
var2 = two
var3 = three
var4 = four

you mention 'If we pass this along to a page that has our code on it'. What code? What instructions go on what page? I really don't understand. I am looking how I can transfer the value of a variable from one page to another page.
(If this comment was disrespectful, please report it.)

 
12/18/2003 5:23:50 AMserpent

well that is how you do it, you lito a page, say, www.mysite.com/vars.php?user=tom&address=mothertown
, this will pass the variables user and address to the page vars.php. you can then read the values of the variables in
the php.

hope that helps anyone who is confused!
(If this comment was disrespectful, please report it.)

 
2/8/2004 10:12:09 PM

thanks, i was pulling my hair out trying to achieve somthing like this
(If this comment was disrespectful, please report it.)

 
2/9/2004 4:37:57 PM

HI, note that as of the new PHP release, register_globals is set to off, and it is advised it remains so. This means that passing bob.php?foobar=99 wont make $foobar available in the target script. You will have to use other ways to get the variable information from the HTTP_GET_VAR list.

hope this helps.
(If this comment was disrespectful, please report it.)

 
11/19/2004 10:45:37 AM

THIS IS PERFECT! i have been setting my vars for so very very long like:
$sort = $HTTP_GET_VARS["sort"];
$mode = $HTTP_GET_VARS["mode"];
$delseq = $HTTP_GET_VARS["delseq"];
this is the best little thing ever!!! it has already saved me so much time!! THANK YOU! :O)
~domspe*
FreePoets.com
(If this comment was disrespectful, please report it.)

 
6/21/2005 12:05:34 AM

I want to know exact difference between $HTTP_POST_VARS[],$HTTP_GET_VARS[].
Which i m not able 2 find
(If this comment was disrespectful, please report it.)

 
8/8/2005 12:35:53 AMDaniel

Thanks a lot for your article, now I understood what I was uncertain about!
(If this comment was disrespectful, please report it.)

 
10/17/2006 1:37:35 AMMurshed Ahmmad Khan

Hey thanks for your article.
Today i did a trick based on your script which saved lot of time.

one of my colleague writes scripts which are coded with register_globals on and when i upload these in a remote server ... i see the scripts is not working as register_globals turned off there...and i have no access to the server....

then i m thinking the workaround ...i added the following codes at the top of every scripts....and they are working pretty fine :-)....niether need to change the script nor to turn on register_globals

while(list($key,$value) = each($_POST))
{
$$key = $value ;
}
while(list($key,$value) = each($_GET))
{
$$key = $value ;
}
?>

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.