GET and POST Method In PHP (GET vs POST)

What are the GET and POST methods in PHP?

The GET and POST Method in PHP are the methods through which a client or a user can send information to the server.

GET and POST Methods are the HTTP request methods used inside the <form> tag to send the different values from the form to the server.

HTTP is the protocol that enables the communication between the client and the server, where a browser can be the client, and an application running on a computer system that hosts your website can be the server.

The GET Method

The GET Method is a PHP super global variable “$_GET“, which is used to collect data from the form after submitting an HTML form with method=”GET”.

GET can also collect data sent in the URL.

Example:

<html>
<body>

<a href="get.php?subject=PHP&web=itsourcecode.com">Test GET</a>

</body>
</html>

When a user clicks the “Test GET” link, the parameters “subject” and “web” are sent to “get.php” file. And then a user can access their values in “get.php” file with “GET” Method.

The example below shows the code in “get.php” file:

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>

When you execute the program, this will be the output:

Study PHP at itsourcecode.com

The POST Method

In the POST method, the data from the HTML form is submitted/collected using a super global variable “$_POST“. This method sends the encoded data from the “form” tag.

Example:

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo "Your Name is: " .$name;
    }
}
?>

</body>
</html>

When you execute the program this will be the output:

POST Method No Action
POST Method No Action

When you click the “submit” button, this will be the result:

POST Method Output
POST Method Output

What is the Difference Between GET and POST Method in PHP?

The difference Between GET and POST methods in PHP is that the GET method sends the information by appending it to the page request.

On the other hand, the POST method sends information via the HTTP header.

PHP is a server-side scripting language designed for web development. The GET and POST methods are two ways for a client computer to send information to the web server.

These methods help to retrieve information from users by forms.

Comparing GET vs POST Method in PHP

GETPOST
BACK button/ReloadHarmlessData will be re-submitted (the browser should alert the user that the data are about to be re-submitted)
BookmarkedCan be bookmarkedCannot be bookmarked
CachedCan be cachedNot cached
Encoding typeapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data
HistoryParameters remain in browser historyParameters are not saved in browser history
Restrictions on data lengthYes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters)No restrictions
Restrictions on data typeOnly ASCII characters allowedNo restrictions. Binary data is also allowed
SecurityGET is less secure compared to POST because data sent is part of the URL

Never use GET when sending passwords or other sensitive information!
POST is a little safer than GET because the parameters are not stored in browser history or in web server logs
VisibilityData is visible to everyone in the URLData is not displayed in the URL
GET vs POST Method in PHP

Conclusion

We have completely discussed what is PHP GET and POST Method and their difference. Also, we provide an example program for the two methods.

I hope this simple tutorial will help you a lot to comply with your requirements in PHP Programming.

By the way, if you have any questions or suggestions about this tutorial, Please feel free to comment below. Thank You!

Leave a Comment