GET vs POST: What is the difference between GET and POST method?

Last updated on Nov 25,2020 58.2K Views
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...

GET vs POST: What is the difference between GET and POST method?

edureka.co

We have two HTTP request methods in PHP for handling the forms, where submitted form-data from users can be collected using these methods. In order to send information to the webserver from the browser client, we use GET and POST methods. 

These methods encode using a scheme called URL encoding before the browser sends the information. Non-alphanumeric characters are replaced with hexadecimal values, and Gaps are removed and replaced with the + character. After encoding the information, it is sent to the server.

let’s get started.

What is GET Method?

It appends form-data to the URL in name/ value pairs. The length of the URL is limited by 2048 characters. This method must not be used if you have a password or some sensitive information to be sent to the server. It is used for submitting the form where the user can bookmark the result. It is better for data that is not secure. It cannot be used for sending binary data like images or word documents. It also provides $_GET associative array to access all the sent information using the GET method.

Example:

<?php if(isset($_GET["name"])){ echo " Welcome," . $_GET["name"] . " "; } ?>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
</head>
<body>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
		<label for="inputName">Name:</label>
		<input type="text" name="name" id="inputName">
		<input type="submit" value="Submit">
	</form>
</body>
</html>

Output –

 

Now let’s move ahead and have a look at the POST method.

What is POST Method?

It appends form-data to the body of the HTTP request in such a way that data is not shown in the URL. This method does not have any restrictions on data size to be sent. Submissions by form with POST cannot be bookmarked. This method can be used to send ASCII as well as binary data like image and word documents. Data sent by the POST method goes through HTTP header so security depends on the HTTP protocol. You have to know that your information is secure by using secure HTTP. This method is a little safer than GET because the parameters are not stored in browser history or in web server logs. It also provides $_POST associative array to access all the sent information using the POST method.

Example:

<?php if(isset($_POST["name"])){ echo " Welcome," . $_POST["name"] . " "; } ?>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
		<label for="inputName">Name:</label>
		<input type="text" name="name" id="inputName">
		<label for="password">password:</label>
		<input type="password" name="pass">
		<input type="submit" value="Submit">
	</form>
</body>
</html>

Output-

Now that you know what are GET and POST methods, let’s have a look at the comparison of GET vs POST method.

What is the difference between GET and POST method?

GET

POST

GET Parameters are included in URL

POST parameters are included in the body

GET requests are often used for fetching documents and GET parameters are used to describe which document we are looking for (or)  what page we are on (or) things of that nature. 

POST parameters are often used for updating data for actually making changes to the server (or) to the data held on the server

Because they are in URL, have a maximum URL length because you can encode many parameters. For eg: Internet Explorer allows 2000 characters in the URL or something like that which can be quite limiting.

By default, they don’t have any maximum length. Now the Server can be configured and most are to have a maximum length but it is usually substantially longer than 2000 characters. 

 When we make a GET request- a simple request for URL. There are a lot of machines between you and server It saves a lot of effort if we know the document has not changed

Post parameters are almost never cached because you are probably updating data on the server so the industry standard is: Don’t cache POST request

 They should not change the server. You should be able to make the same GET request over and the server should not change.

Post requests are okay to change the server. That is what they are generally used for requesting an update for the server and are not cached and there is no maximum length

With this we come to an end of this article, I hope you have learned about GET method, POST method as well the difference between both the methods.

If you wish to know more about PHP, here’s a complete tutorial.

If you found this “get and post method” blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”GET and POST Method in PHP” and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework