PHP GET and POST Methods

A web browser client typically communicates with the web server using one of the two HTTP(Hypertext Transfer Protocol) methods.

  • The GET method
  • The POST Method

These two methods work in two different ways. Before sending the information, the browser encodes the data in a scheme called URL encoding. Name/value pairs are joined with equal signs and different pairs are separated by ampersand(&).

name1=value1&name2=value2&name3=value3

All the black spaces are replaced with + and other nonalphanumeric characters are replaced by hex code before sending the data to the web server.

The GET Method

The GET method sends the data as URL parameters that are usually strings of name and value pairs separated by ampersands (&). In general, URL with GET method looks like as below-

http://www.example.com/action.php?name=sagar&age=29

The encoded data is sent along with the page request separated by the ? character.

  • The GET method is restricted to send upto 1024 characters only.
  • GET can’t be used to send binary data like image, word, and pdf, etc.
  • Sensitive information like username and password should not send using GET method.
  • QUERY_STRING environment variable are used to access the data sent by GET method.
  • PHP provides $_GET associative array to access all the information sent by GET method.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP GET method</title>
</head>
<body>
<?php
if(($_GET["name"] || $_GET["age"])){
echo "<p>Hi, " . $_GET["name"] . "</p>";
echo "<p>You age is: " . $_GET["age"] . "</p>";
}
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<label for="inputName">Age:</label>
<input type="text" name="age" id="inputAge">
<input type="submit" value="Submit">
</form>
</body>

Output

php get method

The POST Method

The POST method sends the data as a package in a separate along with the processing script. In this case, data is not visible to the URL.

  • The POST method does not have any restrictions on the size of the data to be sent.
  • ASCII, as well as binary data, can be sent using the POST method.
  • You can use $_POST associative array to access all the sent information using POST method.
  • The data transferred by the POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP protocol, you can make sure that your information is secure.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"]) || isset($_POST["age"])){
echo "<p>Hi, " . $_POST["name"] . "</p>";
echo "<p>You age is: " . $_POST["age"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<label for="inputName">Age:</label>
<input type="text" name="age" id="inputAge">
<input type="submit" value="Submit">
</form>
</body>

Output

php post method

The $_REQUEST variable

The $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP $_REQUEST method</title>
</head>
<body>
<?php
if(isset($_REQUEST["name"]) || isset($_REQUEST["age"])){
echo "<p>Hi, " . $_REQUEST["name"] . "</p>";
echo "<p>You age is: " . $_REQUEST["age"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<label for="inputName">Age:</label>
<input type="text" name="age" id="inputAge">
<input type="submit" value="Submit">
</form>
</body>

Output

php $_REQUEST variable

 

Please get connected & share!

Advertisement