HTTP GET and POST Methods in PHP
Last Updated :
06 Dec, 2021
In this article, we will know what HTTP GET and POST methods are in PHP, how to implement these HTTP methods & their usage, by understanding them through the examples.
HTTP: The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. A web browser may be the client, and an application on a computer that hosts a website may be the server. A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.
There are 2 HTTP request methods:
- GET: Requests data from a specified resource.
- POST: Submits data to be processed to a specified resource.
We will understand both these methods in detail through the examples.
GET Method: In the GET method, the data is sent as URL parameters that are usually strings of name and value pairs separated by ampersands (&). In general, a URL with GET data will look like this:
Example: Consider the below example:
http://www.example.com/action.php?name=Sam&weight=55
Here, the bold parts in the URL denote the GET parameters and the italic parts denote the value of those parameters. More than one parameter=value can be embedded in the URL by concatenating with ampersands (&). One can only send simple text data via GET method.
Example: This example illustrates the HTTP GET method in PHP.
HTML
<?php
error_reporting(0);
if( $_GET["name"] || $_GET["weight"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['weight']. " kgs in weight.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Weight:<input type="text" name="weight" />
<input type="submit" />
</form>
</body>
</html>
Output:
GET() methodAdvantages:
- Since the data sent by the GET method are displayed in the URL, it is possible to bookmark the page with specific query string values.
- GET requests can be cached and GET requests to remain in the browser history.
- GET requests can be bookmarked.
Disadvantages:
- The GET method is not suitable for passing sensitive information such as the username and password, because these are fully visible in the URL query string as well as potentially stored in the client browser's memory as a visited page.
- Because the GET method assigns data to a server environment variable, the length of the URL is limited. So, there is a limitation for the total data to be sent.
POST Method: In the POST method, the data is sent to the server as a package in a separate communication with the processing script. Data sent through the POST method will not be visible in the URL.
Example: Consider the below example:
POST /test/demo_form.php HTTP/1.1
Host: gfs.com
SAM=451&MAT=62
The query string (name/weight) is sent in the HTTP message body of a POST request.
Example: This example illustrates the HTTP POST method in PHP. Here, we have used the preg_match() function to search string for a pattern, returns true if a pattern exists, otherwise returns false.
HTML
<?php
error_reporting(0);
if( $_POST["name"] || $_POST["weight"] )
{
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] ))
{
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['weight']. " kgs in weight.";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type = "text" name = "name" />
Weight: <input type = "text" name = "weight" />
<input type = "submit" />
</form>
</body>
</html>
Output:
POST() methodAdvantages:
- It is more secure than GET because user-entered information is never visible in the URL query string or in the server logs.
- There is a much larger limit on the amount of data that can be passed and one can send text data as well as binary data (uploading a file) using POST.
Disadvantages:
- Since the data sent by the POST method is not visible in the URL, so it is not possible to bookmark the page with a specific query.
- POST requests are never cached
- POST requests do not remain in the browser history.
Please refer to the Difference between HTTP GET and POST Methods article for the differences between them in detail.
Similar Reads
How to get form data using POST method in PHP ? PHP provides a way to read raw POST data of an HTML Form using php:// which is used for accessing PHPâs input and output streams. In this article, we will use the mentioned way in three different ways. We will use php://input, which is a read-only PHP stream. We will create a basic HTML form page wh
2 min read
Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET
4 min read
How to get information sent via post method in PHP ? In this article, we will learn to get information via the post method in PHP. In PHP, we can use the $_POST method as a superglobal variable that is operated to manage form data. After we click on submit button and the page will send the data through the post method. We can use the data after storin
2 min read
How to access data sent through URL with GET method in PHP ? On a website, we often use forms to collect data, login/signup boxes for users, a search box to search through a web page, etc. So input fields are used to make the website interactive and to collect data. But we know that HTML can not store the data in a database so for that we use a backend script
2 min read
PHP mb_http_input() Function The mb_http_input() is an inbuilt function in PHP that facilitates the HTTP input character encoding to be detected. Syntax: mb_http_input(?string $type = null): array|string|falseParameter: type: This parameter takes values like "G" for GET, "P" for POST, "C" for COOKIE, "S" for string, "L" for the
1 min read