Difference between HTTP GET and POST Methods - GeeksforGeeks

22 Oct, 2021
HTTP GET: The Hypertext Transfer Protocol(HTTP) Get method is mainly used at the client (Browser) side to send a request to a specified server to get certain data or resources. Using this method the server should only let us receive the data and not change its state. Hence it is only used to view something and not to change it. Get method is one of the most used HTTP method. The request parameter of the get method is appended to the URL. Get request is better for the data which does not need to be secure (It means the data which does not contain images or word documents).
Example: In the following HTML code we have created a form with text field as Username and City. we have also included a PHP file getmethod.php where our data would be sent after we click the submit button.
index.html
<!DOCTYPE html><html>  <body>    <form action="getmethod.php" method="GET">        Username: <input type="text"             name="username" /> <br>          City: <input type="text"             name="city" /> <br>                      <input type="submit" />    </form></body>  </html>
In the following PHP code using the GET method we have displayed the Username and city.
 
getmethod.php
<!DOCTYPE html><html>  <body>    Welcome    <?php echo $_GET["username"]; ?> </br>    Your City is:    <?php echo $_GET["city"]; ?></body>  </html>
Output: Data passed in GET method is clearly visible in the address bar, which can compromise the security.
HTTP POST: The Hypertext Transfer Protocol(HTTP) Post method is mainly used at the client (Browser) side to send data to a Specified server in order to create or rewrite a particular resource/data. This data sent to the server is stored in the request body of the HTTP request. Post method eventually leads to the creation of a new resource or updating an existing one. Due to this dynamic use, it is one of the most used HTTP methods. It is not one of the most secure methods because the data that is been sent is included in the body of the request and not in the URL. Post request is better for the data which needs to be secure (It means the data which contains images or word documents).
Example: In the following HTML code we have created a form with text field as Username and Area of study. we have also included a PHP file postmethod.php, where our data would be sent after we click the submit button.
index.html
<!DOCTYPE html><html>  <body>    <form action="postmethod.php" method="post">        Username: <input type="text"             name="username" /> <br>          Area of Study: <input             type="text" name="area" /> <br>          <input type="submit" />    </form></body>  </html>
In the following PHP code using the POST method we have displayed the Username and Area of study .
 
postmethod.php
<!DOCTYPE html><html>  <body>    Welcome    <?php echo $_POST["username"]; ?> </br>    YOur Area of Study is:    <?php echo $_POST["area"]; ?></body>  </html>
Output: Data passed in POST method is not shown in the address bar, which maintains the security.
Difference between HTTP GET and HTTP POST
HTTP GET
HTTP POST
GET request is comparatively better than Post so it is used more than the
Post request.
http
PHP-Questions
Picked
Difference Between
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference Between Method Overloading and Method Overriding in Java
Comparison between Adjacency List and Adjacency Matrix representation of Graph
Difference between Synchronous and Asynchronous Transmission
Difference between LAN, MAN and WAN
Difference between Prim's and Kruskal's algorithm for MST
How to pop an alert message box using PHP ?
PHP in_array() Function
How to Upload Image into Database and Display it using PHP ?
How to pass a PHP array to a JavaScript function?
PHP | Converting string to Date and DateTime