English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to send information to the server using HTTP GET and POST methods, as well as how to retrieve information using PHP.
Web browsers usually use one of two HTTP (Hypertext Transfer Protocol) methods (GET and POST) to communicate with the server. Both methods pass information in different ways and have different advantages and disadvantages, as described below.
In the GET method, data is sent as URL parameters, usually separated by a string of name and value pairs by the ampersand (&). Typically, a URL with GET data looks like this:
http://www.example.com/action.php?name=john&age=24
The bold part in the URL is the GET parameter, and the italic part is the value of these parameters. Multiple parameters=value can be embedded in the URL by connecting with an ampersand (&). Only simple text data can be sent using the GET method.
Since the data sent using the GET method is displayed in the URL, bookmarks can be added to the page using specific query string values.
The GET method is not suitable for transmitting sensitive information, such as usernames and passwords, as this information is completely visible in the URL query string and may be stored in the memory of the client browser as access to the page.
Since the GET method assigns data to server environment variables, the length of the URL is limited. Therefore, there is a limit to the total data that can be sent.
PHP provides a superglobal variable $_GET to access all information sent through the URL or submitted through an HTML form using the method="GET".
<!DOCTYPE html> <html> <head> <title>Online Example PHP GET Method</title> </head> <?php if(isset($_GET[\"name\"])){ echo \/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>
In the POST method, the data is sent to the server as a package in a separate communication with the processing script. The data sent via the POST method is not visible in the URL.
It is more secure than GET because the information entered by the user is never visible in the URL query string or server logs.
There are larger limits on the amount of data that can be transmitted, and text data as well as binary data (file uploads) can be sent using POST.
Since the data sent via the POST method is not visible in the URL, it is not possible to add a bookmark to the page with a specific query.
Similar to $_GET, PHP provides another superglobal variable $_POST to access all information sent via the POST method or submitted via an HTML form using method="POST".
<!DOCTYPE html> <html> <head> <title>Online Example PHP POST Method</title> </head> <?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>
PHP provides another superglobal variable $_REQUEST, which contains the values of the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variable.
<!DOCTYPE html> <html> <head> <title>Online Example PHP $_REQUEST Variable</title> </head> <?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>
You will learn about PHP cookieandForm processingMore information.
Note:Superglobal variables $_GET, $_POST, and $_REQUEST are built-in and always available throughout the entire scope of the script.