Skip to content

Difference between GET and POST method in PHP

postvsget

What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server.

Two HTTP Request Methods commonly used for request-response between a client and server: GET and POST. In this article we are going to understand difference between Get and Post method in php.

The GET Method : Following example which shows you how to use get method.
http://www.example.com/test/demo_form.php?name1=value1&name2=value2

HTTP Get Method

HTTP (Hypertext Transfer Protocol) retrieval method is mainly used on the client (browser) side to send a request to a specific server to retrieve specific data or resources. In this method, the server should only allow us to receive the data and not change its state. Therefore it is only used to see something and not to change it. The Get method is one of the most commonly used HTTP methods. The request parameter of the get method is added to the URL.

Get Prompt works best for data that doesn’t need to be secure (ie data that doesn’t contain images or Word documents).

  • Request can be cached
  • Requests can be bookmarked
  • Requests remain in the browser history
  • Requests have length restrictions
  • Requests should be used only to retrieve data
  • Requests should never be used when dealing with sensitive data
  • Maximum path length of 2,048 characters
  • Data visible to everyone in URL
  • GET is less secure compared to POST because data sent is the URL.

Example: In the following HTML code, we have created a form with a text field such as firstname and lastname. We’ve also included a get.php php file to which our data will be sent after you click the submit button.

// index.html
<!DOCTYPE html>
<html>
<body>
    <form action="get.php" method="GET">
        First Name: <input type="text" name="firstname" /> <br>
        Last Name: <input type="text" name="lastname" /> <br>              
        <input type="submit" />
    </form>
</body>
</html>

In the following PHP code using the GET method we have displayed the First Name and Last Name.

// get.php
<!DOCTYPE html>
<html>  
<body>
    Welcome </br>

    Your First Name is
    <?php echo $_GET["firstname"]; ?> </br>
    And Your Last Name is:
    <?php echo $_GET["lastname"]; ?>
</body>
</html>

HTTP Post Method

HTTP Post (Hypertext Transfer Protocol) method is mainly used on the client (browser) side to send data to a specific server to create or rewrite a specific resource/data. This data is sent to the server and stored in the request body of the HTTP request. Finally, the Post method results in creating a new resource or updating an existing one. Because of this dynamic usage, it is one of the most widely used HTTP methods.

The Post request works best for data that needs to be secure (i.e. data that contains images or text documents).

  • Requests are never cached
  • Requests do not remain in the browser history
  • Requests cannot be bookmarked
  • No restriction data length
  • Requests have no restrictions on data length
  • Data is not displayed in URL
  • POST is a little safer than GET because the parameters are not stored in browser history.

Example: In the following HTML code, we have created a form with a text field such as firstname and lastname. We’ve also included a PHP post.php file that our data will be sent to after you click the submit button. We are doing same example what we already did with get method. Now we are doing same with post method for better understand.

// index.html
<!DOCTYPE html>
<html>
<body>
    <form action="post.php" method="post">
        First Name: <input type="text" name="firstname" /> <br>
        Last Name: <input type="text" name="lastname" /> <br>
        <input type="submit" />
    </form>
</body>  
</html>

In the following PHP code using the HTTP POST method, we have displayed the First Name and Last Name.

// post.php
<!DOCTYPE html>
<html>  
<body>
    Welcome </br>
    Your First Name is
    <?php echo $_POST["firstname"]; ?> </br>
    And Your Last Name is:
    <?php echo $_POST["lastname"]; ?>
</body>
</html>

Related Articles

  1. Set session in CodeIgniter
  2. WordPress vs Joomla
  3. How to install Gulp 4 with sample project
  4. Why all sites now require SSL (https)