GET vs POST

GET Vs POST

The client browser uses two methods to send information from the client to the webserver. These methods are GET and POST. However, before sending the information, the browser first encodes the information using the coding scheme called URL encoding.

In this coding scheme, key-value pairs, which are here name-value pairs, which are here name-value pairs, are combined together in such a way that different name-value pairs are separated by & (ampersand). Any whitespaces are replaced by the plus sign(+) and non-alphanumeric characters are replaced with their hexadecimal values. Once the encoding process is over, the data is sent to the webserver.

The GET Method

When the GET method is used for sending information to the web server, it sends the same along with the page URL. The URL is appended by the encoded information and the question mark sign (?) separates the former from the latter. Some of the key things to note about the use of this method include –

  • Since this method appends the page URL, the sent information will also appear in the logs of the server.
  • The maximum number of characters that can be sent using the GET method is 1024 characters.
  • There is an inherent issue of compromised confidentiality with this method. Therefore, its use must be avoided when you are sending passwords or critical information to the webserver.
  • Another inherent limitation of the GET method is that documents and images cannot be sent to the server using this method.
  • Besides the above-mentioned, it is also possible to access the data sent using this method by retrieving the QUERY_STRING environment variable. Therefore, data is not safe.
  • It is possible to access all the sent information using this method by accessing $_GET, which is an associative array provided by PHP for this purpose.

You can try the sample code given below to understand how this method is used.

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

The POST Method

An alternative to the GET method is the POST method. This method makes uses of HTTP headers to transfer information from the client to the server. The encoded information is out into the header. Some of the things that you must know about the use of this method for information transfer has been listed below-

  • Unlike the GET method, there is no limitation on the number of characters that can be sent using this method.
  • It is possible to send binary as well as ASCII data using this method.
  • As far as the security of data is concerned, data security totally lies in the hands of the HTTP protocol. You can ensure higher security of data by using HTTP secure.
  • All the data using the POST method can be accessed using the $_POST associative array.

You can try the sample code given below to understand how this method is used.

<?php
if($_POST[“name”] || $_POST[“age”]){
if(preg_match(“/[^A-Za-z’-]/”,$_POST[‘name’])){
die(“Name is invalid”);
}
echo “Welcome”. $_POST[‘name’].”<br />”;
echo “You are”. $_POST[age].”years old”;
exit();
}
?>
<html>
<body>
<form action=”<?php $_PHP_SELF?>” method=”GET”>
Name: <input type=”text” name=”name” />
Age: <input type=”text” name=”age” />
<input type=”submit”>
</form>
</body>
</hml>
Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy