learn

GET, POST and REQUEST Variables

category: Basics
Created by: Dr.Ferrous

GET, POST and REQUEST Variables

category: Basics
Created by: Dr.Ferrous
twitter google+ facebook pinned 
The method attribute of a form can have two values: GET and POST. The difference between GET and POST methods lies in how the information is transmitted to the PHP script.

$_GET

The $_GET variable is a superglobal Array that contains data from a form sent with method="get" or from URL.
Information sent from a form with the GET method will be displayed in the browser's address bar (so, is visible to everyone) and has limits on the amount of data to send (about 2,048 characters).
Example:

After the user fills the form and clicks the "submit" (Send) button, the URL sent to the server could look something like this:

http://phpwin.org/script.php?user=MarPlo&gen=man
 

The "script.php" file can use the $_GET variable /array to collect form data (the names of the form fields are the keys in the $_GET array).

Before using data received through get (or post), is indicated to check if they were set /sent (using the "isset()" function).

If you want to use in a PHP script data sent directly from a link, that data must be added in the URL address after the "?" character, in key=value pairs separated by "&" character.
Example:

 

This link example has an URL with two keys in the query string (after the "?" character), one called src and the other called id. In the "index.php" file you can manipulate these values. All you have to do is reference the key within the $_GET.

$_POST

The $_POST variable is a superglobal Array that contains data from a form sent with method="post".
Information sent from a form with the POST method is invisible to the user and hasn't a specified limit on the amount of information to send. By default, PHP permits up to 8MB of POST data (can be changed from the post_max_size in the php.ini file).
Example:

 

When the user clicks the "submit" (Send) button, script.php is called, the URL will look like this:


http://phpwin.org/script.php

The "script.php" file can use the $_POST variable /array to collect form data (the names of the form fields are the keys in the $_POST array).

$_REQUEST

The PHP $_REQUEST variable is a superglobal Array that contains the contents of both $_GET, $_POST, and $_COOKIE arrays. It can be used to collect data sent with both the GET and POST methods.
Example:

This code submits a form via the POST method, and when the form is posted, the code sends some values along the URL via the GET method ( "id=789", defined in the address from "action" attribute). The URL will look like this:


http://phpwin.org/script.php?id=789

The sent data are loaded in the superglobal $_REQUEST variable /array, and can be used to collect both GET and POST data. For example:

 

If there are data received via GET and POST at the same time (through URL address and from a form), with the same "name" to both ($_GET['name'] and $_POST['name'), the $_REQUEST['name'] will contain the value of $_POST['name'].
Example: