HTTP GET and POST Methods in PHP

What is 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 web site 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 two HTTP request methods: GET and POST

  1. GET – Requests data from a specified resource.
  2. POST – Submits data to be processed to a specified resource.

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:

Example :

http://www.example.com/action.php?name=Sam&weight=55



Explanation :
  1. The bold parts in the URL are the GET parameters and the italic parts are the value of those parameters.
  2. More than one parameter=value can be embedded in the URL by concatenating with ampersands (&).
  3. One can only send simple text data via GET method.

Input :

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   if( $_GET["name"] || $_GET["weight"] ) {
      echo "Welcome ". $_GET['weight']. "<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>
chevron_right

Input:


Advantages of Using the GET Method

Disadvantages of Using the GET Method

The POST Method
In POST method the data is sent to the server as a package in a separate communication with the processing script. Data sent through POST method will not be visible in the URL.

EXAMPLE :

POST /test/demo_form.php HTTP/1.1
Host: gfs.com
SAM=451&MAT=62

Explanation

The query string (name/weight) is sent in the HTTP message body of a POST request.

Input :

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   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>
chevron_right

Output :


Advantages of using POST Method

Disadvantages of using the POST Method





I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :