GET vs POST

GET vs POST

There are two common methods by which browser client can send information web server.

GET Method
POST Method

Both GET and POST create an array which holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

$_GET is an array of variables passed to the current script via the URL parameters. Before the browser sends the information, it encodes it using a scheme called URL encoding. In this scheme, name/value pairs are joined with equal signs and different pairs are separated by the ampersand(&). e.g. http://www.shoutme360.com/index.htm?name1=value1&name2=value2

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

Note-
The GET method is restricted to send information upto 1024 characters only.

Never use GET method if you have password or other sensitive information to be sent to the server.

GET can’t be used to send binary data, like images or word documents, to the server.

The data sent by GET method can be accessed using QUERY_STRING environment variable.

The PHP provides $_GET associative array to access all the sent information using GET method.

<?php
if( $_GET[“name”] || $_GET[“gender”] ) {
echo “Welcome “. $_GET[‘name’]. “<br />”;
echo “Your gender is “. $_GET[‘gender’];

}
?>
<html>
<body>

<form method = “GET”>
Name: <input type = “text” name = “name” />
Gender: <input type = “text” name = “gender” />
<input type = “submit” />
</form>

</body>
</html>

$_POST is an array of variables passed to the current script via the HTTP POST method. Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Note-
The POST method does not have any restriction on data size to be sent.

The POST method can be used to send ASCII as well as binary data.

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.

The PHP provides $_POST associative array to access all the sent information using POST method.

<?php
if( $_POST[“name”] || $_POST[“gender”] ) {
echo “Welcome “. $_POST[‘name’]. “<br />”;
echo “Your gender is “. $_POST[‘gender’];

exit();
}
?>
<html>
<body>

<form method = “POST”>
Name: <input type = “text” name = “name” />
Gender: <input type = “text” name = “gender” />
<input type = “submit” />
</form>

</body>
</html>

Leave a Reply