Get vs Post in PHP – Free PHP Tutorials

Before sending to data to the server page for handling and operation, the page at client side performs some kind of URL encoding. The values of the corresponding variables/names are binded to each other in some way.

POST and GET are two methods are used for URL encoding and retrieving data at the server side form the client side. The information from the browser of the client’s computer is sent on submission of the page to the handling script at the server-side for performing checks and operation on the data received.


GET METHOD

This is one of the basic and simple methods of sending the information to the server side script. In this method, the values that are sent are visible on the address/URL request of the page as a very long string text. Therefore this method is not considered to be the safest method to send data. There is one limitation to this method is that the size of data that can be sent is fixed i.e. 1024 characters only.

Now the values sent from the client’s page are stored in a global variable array. The global variable array is $_GET which is an associative array.  The values of the variable are retrieved from this array at the server side.

One must keep in mind that this method is not secure. Therefore when it comes to sending data like passwords and personal information this method should not be used.

For example, look at the following code for understanding:-

<?php
   if( $_GET["name"]) {
      echo "Welcome to Padhle.com";
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "GET">
         Name: <input type = "text" name = "name" />
                 <input type = "submit" />
      </form>
      
   </body>
</html>

POST METHOD

In this method, the data sent via the HTTP header is secure. The values are not visible on the header page request. The values can also be sent in binary or ASCII format. This method provides security, unlike the GET method.

In this method, there is no limitation in the size of the value. It supports many other data types other than string. Hence it is much more handy and convenient to use this method over $_GET.

Also in this method, the values of the variables received are stored in a superglobal variable array i.e. $_POST. This array is also an associative array i.e. the information is stored in a key-value pair.

For example look at the code below for better understanding:

<?php
   if( $_POST["name"]) {
      echo "Welcome to Padhle.com";
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "POST">
         Name: <input type = "text" name = "name" />
                 <input type = "submit" />
      </form>
      
   </body>
</html>

NOTE: In PHP we have one more superglobal variable $_REQUEST. This variable stores the values of $_GET, $_POST, and cookies. One can also use this method to retrieve the values received from the client page.

For example look at the following example:

<?php
   if( $_GET["name"]) {
      echo "Welcome to Padhle.com";
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "GET">
         Name: <input type = "text" name = "name" />
                 <input type = "submit" />
      </form>
      
   </body>
</html>

For any doubts and queries, leave your comments in the section given below.

Leave a Comment

Your email address will not be published. Required fields are marked *