PHP GET vs POST: Difference, Examples & When to Use

Introduction

When building dynamic websites, developers often need to send data from a client (browser) to the server. In PHP, this is commonly done using HTTP request methods. The two most commonly used methods are:

  • GET
  • POST

Both methods are used to send data from a form to the server, but they work in different ways and are used in different situations.

What is the GET Method in PHP?

The GET method is used to send data to the server through the URL. The data is appended to the URL in the form of query parameters.

These parameters appear after a question mark (?) in the URL.

Example URL:

  • name and age are parameters
  • Their values are Rahul and 21

In PHP, GET data is accessed using the $_GET superglobal array.

Syntax of GET Method


$_GET['parameter_name']

Example of GET Method

HTML Form


<form method="get" action="process.php">
Name: <input type="text" name="name">
<input type="submit">
</form>

PHP File (process.php)


$name = $_GET['name'];
echo "Welcome " . $name;

If the user enters Rahul, the URL will look like: process.php?name=Rahul

Output

Welcome Rahul

Explanation

Here:

  1. The form uses method=”get”
  2. The data is sent through the URL
  3. PHP retrieves the value using $_GET

Advantages of GET Method

  1. Simple and easy to use
  2. Data can be bookmarked
  3. URLs can be shared
  4. Good for search queries

Example:


example.com/search.php?keyword=php

Disadvantages of GET Method

  1. Data is visible in the URL
  2. Not secure for sensitive information
  3. Limited data length (URL size restriction)
  4. Not suitable for large data

What is the POST Method in PHP?

The POST method is used to send data to the server through the HTTP request body, not through the URL.

This means the data is not visible in the address bar.

POST is commonly used when submitting:

  1. Login forms
  2. Registration forms
  3. File uploads
  4. Payment forms

In PHP, POST data is accessed using the $_POST superglobal array.

Syntax of POST Method


$_POST['parameter_name']

Example of POST Method

HTML Form

 
<form method="post" action="process.php">
Name: <input type="text" name="name">
<input type="submit">
</form>

PHP File (process.php)


$name = $_POST['name'];
echo "Welcome " . $name;

If the user enters Rahul, the URL remains: process.php

Output

Welcome Rahul

Explanation

  1. The form uses method=”post”
  2. Data is sent through the HTTP body
  3. PHP accesses the data using $_POST

Advantages of POST Method

  1. More secure than GET
  2. Data is not visible in the URL
  3. Can send large amounts of data
  4. Supports file uploads

Disadvantages of POST Method

  1. Data cannot be bookmarked
  2. Cannot easily share form data through URL
  3. Slightly slower than GET in some cases

Difference Between GET and POST

The GET and POST methods have several important differences.

Feature GET POST
Data Location URL HTTP request body
Visibility Visible in URL Not visible
Security Less secure More secure
Data Size Limited Large data allowed
Bookmarking Supported Not supported
File Upload Not supported Supported

Real Life Example

1. Search Form (GET)

Search forms usually use the GET method.

Example:


google.com/search?q=php

The query appears in the URL, which makes it easy to share and bookmark.

2. Login Form (POST)

Login forms should always use the POST method.

Example:


<form method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit">
</form>

Using GET here would expose the password in the URL, which is not secure.

Example Combining GET and POST


if($_SERVER["REQUEST_METHOD"] == "POST"){
   echo "Data received using POST method";
}
if($_SERVER["REQUEST_METHOD"] == "GET"){
   echo "Data received using GET method";
}

This code checks which method is used to send the request.

Conclusion

Both GET and POST methods are essential for sending data from the client to the server in PHP applications. The GET method sends data through the URL, making it useful for search queries and shareable links. However, it is less secure and has limitations on data size.

On the other hand, the POST method sends data through the HTTP request body, which makes it more secure and suitable for sensitive information such as login credentials and form submissions.

GET vs POST method? – Objective Questions (MCQs)

Q1. GET method sends data via ______.






Q2. POST method sends data via ______.






Q3. Which method is more secure for sensitive data?






Q4. Which method has size limitations?






Q5. Which method is bookmarkable?