GET vs POST: What is the difference between getting and POST methods?
Quick java tutorial on the Difference between GET and POST methods in HTTP. Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from client to server in HTTP protocol.
Compare GET vs POST |
The following table compares the two HTTP methods: GET and POST.
GET | POST | |
BACK button/Reload | Harmless | Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted) |
Bookmarked | Can be bookmarked | Cannot be bookmarked |
Cached | Can be cached | Not cached |
Encoding type | application/x-www-form-urlencoded | application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data |
History | Parameters remain in browser history | Parameters are not saved in browser history |
Restrictions on data length | Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) | No restrictions |
Restrictions on data type | Only ASCII characters allowed | No restrictions. Binary data is also allowed |
Security | GET is less secure compared to POST because data sent is part of the URL
Never use GET when sending passwords or other sensitive information! |
POST is a little safer than GET because the parameters are not stored in browser history or in web server logs |
Visibility | Data is visible to everyone in the URL | Data is not displayed in the URL |
GET | POST |
1) In the case of Get request, only a limited amount of data can be sent because data is sent in the header. | In case of post request, a large amount of data can be sent because data is sent in body. |
2) Get request is not secured because data is exposed in the URL bar. | Post request is secured because data is not exposed in the URL bar. |
3) Get requests can be bookmarked. | Post requests cannot be bookmarked. |
4) Get a request is idempotent. It means the second request will be ignored until the response of the first request is delivered | Post request is non-idempotent. |
5) Get request is more efficient and used more than Post. | Post request is less efficient and used less than getting. |
Methods of Sending Information to Server
A web browser communicates with the server typically using one of the two HTTP (Hypertext Transfer Protocol) methods — GET and POST. Both methods pass the information differently and have different advantages and disadvantages, as described below.
The GET Method
In 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:
http://www.example.com/action.php?name=john&age=24
The bold parts in the URL are the GET parameters and the italic parts are 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.
Advantages and Disadvantages of Using the GET Method
- 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.
- 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.
PHP provides the superglobal variable $_GET to access all the information sent either through the URL or submitted through an HTML form using the method=”get”.
Example
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>
The 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 visible in the URL.
Advantages and Disadvantages of Using the POST Method
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.
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.
Like $_GET, PHP provides another superglobal variable $_POST to access all the information sent via post method or submitted through an HTML form using the method=”post”.
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>
The $_REQUEST Variable
PHP provides another superglobal variable $_REQUEST that contains the values of both the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variable.
Example:
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 more about PHP cookies and form handling in the advanced section.
Note: The superglobal variables $_GET, $_POST, and $_REQUEST are built-in variables that are always available in all scopes throughout a script.
You will learn more about PHP cookies and form handling in the advanced section.
Note: The superglobal variables $_GET, $_POST and $_REQUEST are built-in variables that are always available in all scopes throughout a script.
Related:
- Json in js | Get value from json object in javascript
- how to get data from json array in php | json in php
- What is Encode and Decode JSON Data in PHP | JSON in php
- Javascript interview Queand & Ans 2022 Beginners
- How to send email from PHP without SMTP server
- PHP Basic Interview Questions and Answers in Hindi
- difference between echo, print, and print_r in PHP
You may like GET vs POST
- PHP Array: Indexed,Associative, Multidimensional
- PHP remove duplicates from multidimensional array
- Create Modern Website Using HTML CSS
- How to Convert String to Array in PHP
- Data types in php | php data types in hindi
- How to Learn Programming Languages step by step | What this Programming
GET vs. POST … HTTP POST requests supply additional data from the client (browser) to the server in the message body. In contrast, GET requests to include all.
great…………………….
Very Nice Website…
nicc and usefull website
This comment has been removed by the author.
Good job! For Sharing your best ideas… I will refer the people to best learning and training Institute for online courses…
Good Job! You have Shared your well knowledge… I will refer the people to the best IT Solutions providers
Great! You have shared your good knowledge… I will refer people to the best IT solution providers
Nice You have shared your good knowledge Thanks