PHP is a very famous programming language among developers these days. As PHP mainly used for developing rich web applications, so its getting popular day by day among Users and Corporate Clients. Since from the inception PHP provides simple and easy to learn syntax to developers. It also facilitates two HTTP methods(GET and POST), which is used for passing form data to the web server. However implementation of both HTTP methods is same but difference lies in its working. So, this article explains the difference between GET and POST methods with respective examples.
Note: Both HTTP methods are case insensitive in the HTML, but PHP manual recommend Uppercase syntax for both HTTP methods.
Get and Post in Php
GET Method
GET method is one of the common HTTP method used for passing form data to the web server. It also terms as a superglobal array. GET method uses query string to pass form data to the web server. For example:
http://mydomain.com/test.php?name=somename&age=sixteen
Above mentioned query string composes few well known characters to pass data to the web server. As we can see in this query string, that it begins with ” ? ” and passing specific values to variables. Now here we can also pass more values to variables by appending ampersand sign ” & ” to the end of first value.
Sample PHP form using GET method
HTML Form
<form method="GET" action="somefile.php">
<label>Name:</label><input type="text" name="u1" /><br />
<label>Age:</label><input type="text" name="a1" /><br />
<input type="submit" name="sub" />
</form>
PHP Script (somefile.php)
<?php
$x = $_GET['u1']; // assigning GET value to variable.
$y = $_GET['a1'];
echo "Hey ".$x."your age is: ".$y; // printing passed data
?>
Output query string:
http://testingdomain.com/getandpost.php?u1=neeraj&a1=87&sub=Submit+Query
The most common difference between GET and POST in php is the query string, where all form parameters are encoded in URL with GET request. Any user can easily bookmark GET queries while this is not easy with POST method.
POST Method
POST method is the second HTTP method used for passing data to the web server. As POST request passes the form parameters in the body of the HTTP request so URL won’t affected by this method. For Example:
http://mydomain.com/test.php
Above mentioned query string won’t changed by POST method while exchanging form data with web server. However, there are few browser extension and web application available in the market, which helps user to bookmark POST data in the browsers.
Sample PHP form using POST method
HTML Form
<form method="POST" action="somefile.php">
<label>Name:</label><input type="text" name="u1" /><br />
<label>Age:</label><input type="text" name="a1" /><br />
<input type="submit" name="sub" />
</form>
PHP Script (somefile.php)
<?php
$x = $_POST['u1']; // assigning POST value to variable.
$y = $_POST['a1'];
echo "Hey ".$x."your age is: ".$y; // printing passed data
?>
Output query string:
http://testingdomain.com/getandpost.php
GET requests are idempotent as per HTTP specifications which means one GET request for a particular URL(including form parameters) is the same as two or more requests for that URL. However, Browsers can cache the response pages for GET requests because response page doesn’t change upon subsequent page refresh.
Note: Don’t send any sensitive information(like username, password etc) with GET method to the web server. Always use GET method for small queries.
While on the other side POST requests are not idempotence, which means this cannot be cached by browsers and application need to re-connected to the web server every time the page is displayed. POST requests are mostly used in ecommerce applications, banking website, or other database centric applications where response pages may changes very frequently.
Note: Always use POST method for sending sensitive information to the web server. As this confirms user data would be safe while transferring to the server. Collectively $_GET and $_POST superglobal arrays is used to access form parameters from our PHP code.
Uploaded by: Author
Data is not safe when you use POST request, it’s just safer, not safe.
These are mostly just differences between GET and POST requests in general, not in PHP.