Info
Content

GET/POST variables

GET and POST are two methods used to request data as part of the HTTP protocol.

GET variables

GET variables are sent using the query string of a URL, for example https://www.example.com?qs=banana&ss=lemon. These values can be specified when calling a PHP script as with everything else.

$_GET["item"];
POST variables

POST variables are not passed through the query string, instead requesting that a web server accepts the data enclosed in the body of the request message and uses that. They are accessed in a very similar way to GET variables.

$_POST["item"];
Checking if a GET/POST variable has been specified

If a GET/POST variable is not specified in the request, and is referenced by the program, an error will be thrown. To combat this we can use the below code.

if (!array_key_exists("item", $_GET) || $_GET["item"] == "") { 
	// either the item was not specified or was empty
	echo "item not specified";
    die();
} else {
	// the item was present and correct
	$thingy = $_GET["item"];
}
No Comments
Back to top