PHP GET and POST are predefined global and associated array variables used for retrieving user submitted information passed using HTTP GET and HTTP POST methods. They are mostly used with PHP web form applications where you need to interact with your user input. This tutorial is a complete guide to using PHP GET and POST functions with illustrative examples and security considerations.
PHP $_GET works similarly to $_POST. It is used when:
The web form is using HTTP GET Method.
You need to pass the value in the query string variable of URL. This makes the submitted value visible to the users.
You need to make the URL available for future use such as the ability to be bookmarked.
You only need to pass a limited amount of characters. Since the value of $_GET gets appended in the URL as a value of the query string variable, the limitation depends on the URL limit which is around 2083 characters: http://support.microsoft.com/kb/208427.
Like $_POST, $_GET is also a predefined associated array variable with global scope.
Basic Example (basicget.php):
<html> <head> <body> <title>Basic Application for PHP $_GET</title> </head> <form action="processor.php" method="get"> Please enter your favorite food: <input name="favoritefood" type="text" /> <input type="submit" name="submit" value="Submit this form"> </form> </body> </html>
The PHP script(form handler/processor):
<?php //retrieve favorite food from the web form which is submitted using HTTP GET Method $favoritefood= $_GET['favoritefood'];
//Output user favorite food back to the browser echo $favoritefood; ?>
When the sample code is run, the passed value of the form will be appended in the URL:
In the above URL, the query string variable is “favoritefood” with the submitted value of “italian pasta” using the HTTP GET method. You can re-use this URL by bookmarking because the submitted value is appended in the URL.
Warning: Do not use the above sample code for practical applications because it is not secure. See last section of this tutorial.