PHP GET and POST

Methods of Sending Information to Server

Web browsers communicate with servers primarily using two HTTP (Hypertext Transfer Protocol) methods: GET and POST. Both methods handle data differently and come with distinct advantages and disadvantages, as outlined below.

The GET Method

In the GET method, data is transmitted as URL parameters, typically in the form of name-value pairs separated by ampersands (&). A typical URL with GET data looks like this:

http://www.example.com/action.php?name=john&age=24

In this URL, the bold parts represent the GET parameters, while the italic parts denote their corresponding values. Multiple parameter=value pairs can be included in the URL by concatenating them with ampersands (&). The GET method is suitable only for transmitting simple text data.

Advantages and Disadvantages of Using the GET Method

  • GET method data are displayed in the URL, allowing users to bookmark pages with specific query string values.
  • However, GET is unsuitable for transmitting sensitive information like usernames and passwords, as these are visible in both the URL query string and potentially stored in the client browser's memory.
  • Additionally, the length of the URL is limited, imposing constraints on the amount of data that can be sent.

PHP provides the $_GET superglobal variable to access all information sent via URL or submitted through an HTML form using method="get".

Example

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

The POST Method

In the POST method, data is sent to the server as a package in a separate communication with the processing script. Unlike GET, data sent through POST method is not visible in the URL.

Advantages and Disadvantages of Using the POST Method

  • POST is more secure than GET because user-entered information is never visible in the URL query string or server logs.
  • There's a much larger limit on the amount of data that can be transmitted, and POST supports sending both text and binary data (such as file uploads).
  • Since POST data is not visible in the URL, it's not possible to bookmark the page with specific query parameters.

Similar to $_GET, PHP provides another superglobal variable $_POST to access all information sent via POST method or submitted through an HTML form using method="post".

Example

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

The $_REQUEST Variable

PHP includes another superglobal variable $_REQUEST that consolidates the values from $_GET, $_POST, and $_COOKIE variables.

Example

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

You'll delve deeper into PHP cookies and form handling in the advanced section.

Note: The superglobal variables $_GET, $_POST, and $_REQUEST are predefined variables that remain accessible in all parts of a script.