Get & Post

PHP provides two methods through which a client (browser) can send information to the server. These methods are given below, and discussed in detail:

  1. GET method
  2. POST method

 

The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

http://www.example.com/action.php?name=amit&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.

Example:

<?php
   if( $_GET["name"] || $_GET["age"] ) 
   {
      echo "Welcome ". $_GET['name']. "<br />";
      echo "You are ". $_GET['age']. " years old.";
      exit();
   }
?>
<html>
   <body>
   
      <form action="" method="GET">
         Name: <input type = "text" name = "name" />
         Age: <input type = "text" name = "age" />
         <input type = "submit" />
      </form>
      
   </body>
</html>

 

The POST Method

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

 

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 specific query.

Example:

<?php
   if( $_POST["name"] || $_POST["age"] ) {
      echo "Welcome ". $_POST['name']. "<br />";
      echo "You are ". $_POST['age']. " years old.";
      exit();
   }
?>
<html>
   <body>
   
      <form action="" method="POST">
         Name: <input type = "text" name = "name" />
         Age: <input type = "text" name = "age" />
         <input type = "submit" />
      </form>
   
   </body>
</html>