article

Using $HTTP_POST_VARS and $HTTP_GET_VARS

Email
Submitted on: 1/1/2015 8:20:00 AM
By: bleh  
Level: Beginner
User Rating: By 12 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 90
 
     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.

 
				
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 3 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


 There are no comments on this submission.
 

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.