PHP and MySQL (48 Blogs) Become a Certified Professional

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

Published on Jul 16,2019 791 Views
Aayushi Johari
A technophile who likes writing about different technologies and spreading knowledge. A technophile who likes writing about different technologies and spreading knowledge.
GET vs POST: What is the difference between GET and POST method?

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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 web server from the browser client, we use GET and POST methods. 

  • GET Method: Data is requested from a specific resource
  • POST Method: Data is submitted to be processed to a specific resource

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 –

       

      getmethod - get and post method - Edureka

      get method - get and post method - Edureka

       

      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-

      post method - get and post method - Edureka

      post - get and post method - Edureka

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

       

      GET VS POST: Difference between GET Method 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.

      Comments
      0 Comments

      Browse Categories

      Subscribe to our Newsletter, and get personalized recommendations.