GET vs POST in php
This post will explain the GET vs POST in PHP with examples in php. Communication between client and server is enabled by HTTP [HyperText Transfer Protocol] which works in request and response method. Here the client is the web browser and server is the application that hosts the website. The different types of HTTP requests used in PHP are GET vs POST in PHP.
GET Method
GET method is the method used for web request that pass data through website URL. It sends all form data as encrypted data or as raw data appended to action URL as parameter string. The webpage and this parameters is separated with ? (Question mark) symbol. Multiple Parameters are passed through URL separated by & (and) symbol. Parameters like value1 and value2 are sent as request.
For example: http://www.websitename.com?key1=value1&key2=value2…….
Points to know about GET method are given as follows:
- Allows the data from a specified resource
- Data sent through this request is stored in web browser history.
- It have length restrictions since it is passed by URL. For example: Internet Explorer allows 2000 characters to pass through the URL or something like that which is quite limited.
- It can be bookmarked and cached.
- Second request will not be accepted until response of the first request is received. So duplication of records can be avoided.
- This method have security restrictions to pass sensitive data over this request. By robots and crawlers, the GET may be used arbitrarily is one of the reason, which would not considered as a the request method with no side effects.
- This method is more efficient than post method.
POST Method
POST method is the most common request method used in php where data sent to the server is stored in the body of the HTTP request. When compared to GET vs POST in PHP this method is a little safer than GET since security depends on the HTTP protocol.
Point to know about POST method:
- Submits the data which was processed to a specified resource.
- In POST method data sent are not cached or saved in browser history
- Data request can’t be bookmarked.
- The restriction of sending limiting data in GET request method is overcome by this POST method.
- This POST method is the most reliable request method to send important data to the server. If you are the one to upload large volume of data to server or in need to upload various data type it is the most trusted method.
- It is less efficient than get method.
- Difficult to hack the data .
EXAMPLE:
Test.php have the following code: here user select values and submit form. Once submitted it passes the value to the request page through the URL.
<html> <head></head> <body> <b> Search Your Query:</b><br> <FORM action="request.php" method = "GET"> <input type="radio" name="key2" value="Value1" checked>Value 1 <input type="radio" name="key2" value="Value2">Value 2 <input type="text" name="key1" size="20" maxlength="120"> <input type="submit" value="Search"><br> </FORM> </body> </html>
Selected values are submitted to the request.php with values in URL.
If the same code in test .php use POST method to send data
<html> <head></head> <body> <b> Search Your Query:</b><br> <FORM action="request.php" method = "GET"> <input type="radio" name="key2" value="Value1" checked>Value 1 <input type="radio" name="key2" value="Value2">Value 2 <input type="text" name="key1" size="20" maxlength="120"> <input type="submit" value="Search"><br> </FORM> </body> </html>
Then, data passed to the server with body request,not in a explicit manner which ensures security in the communication process.
Comparisons: PROS and CONS
In POST method we can’t say that there are no side effects. Here repeating the query is often expected that results in creating two messages instead of one. Re submission of data will happen while refreshing the request page.
Beside keeping the URL relatively cleaner, POST method also lets you to send much more information (as URLs are limited in length, for all practical application purposes), and also allow you to send any type of data such as binary data, images and other files.(for example, for file upload using form, we can’t use GET — they have to use POST with a special content type/encoding option).
Difference in processing
Processing a submitted form data depends on the method (METHOD=”GET” or METHOD=”POST”) through which data is sent. Since different ways are used in encoding the data, different decoding mechanisms are used. In simple, changing the method will change the script that process the data submitted. For example, In CGI interface — in Get method, the script receives the data as query string. But in POST method, data from the form is passed as standard input stream (stdin) and the Content-length header gives the number of bytes to be read.
Obviously web servers will keep the log of entire URL in plain text in their access logs. Of course sending sensitive data like passwords and other information over GET is not a good idea. It is applicable irrespective of whether HTTP or HTTPS (HTTP over TLS/SSL)is used.
This is all about GET vs POST in PHP.