PHP & Frameworks

Difference between GET and POST method in PHP

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. What Is MVC Architecture? And Why Should You Care?
  3. What Is Join And Types Of Join In Mysql
  4. WordPress vs Joomla
  5. How to install Gulp 4 with sample project
  6. Why all sites now require SSL (https)
Developer Diary

Share
Published by
Developer Diary

Recent Posts

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

2 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

3 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

3 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

3 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

5 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

7 months ago