PHP GET and POST Methods

11/21/2025
All Articles

PHP GET and POST method example for handling HTML forms

PHP GET and POST Methods

PHP GET and POST Methods: A Complete Beginner’s Guide

When working with HTML forms and web applications, two of the most important concepts in PHP are the GET and POST methods. These HTTP request methods determine how data is sent from the browser to the server — and how PHP receives and processes that data.

This guide explains the difference between GET and POST, how they work, when to use which method, and includes clear developer examples.


What Are GET and POST Methods in PHP?

GET and POST are the two most commonly used HTTP request methods for sending data from a client (browser) to a server.

Method Data Sending Style Use Case
GET Sent through URL Read-only or search requests
POST Sent inside request body Secure form submissions

1. PHP GET Method

The GET method appends form data directly in the URL as query parameters.

Example URL:

https://example.com/search.php?query=php+tutorial

When GET is Used:

  • Search forms

  • Filters & sorting

  • Pagination (page=1, page=2, …)

  • Passing small & non-sensitive data

Advantages of GET

  • Simple and easy to test

  • Bookmarkable URLs

  • Can be cached by browser

Limitations of GET

  • Data visible in the URL

  • Not secure for sensitive information

  • URL length limit


GET Method – Developer Example

HTML Form (search.html)

<form action="search.php" method="GET">
  <label>Search:</label>
  <input type="text" name="query">
  <button type="submit">Search</button>
</form>

PHP Processing (search.php)

<?php
if (isset($_GET['query'])) {
    $search = htmlspecialchars($_GET['query']);
    echo "You searched for: " . $search;
}
?>

2. PHP POST Method

The POST method sends data securely inside the request body — NOT visible in the URL.

When POST is Used:

  • Login forms

  • User registration

  • Payment forms

  • File uploads

  • Storing sensitive data

Advantages of POST

  • More secure

  • Can send large amounts of data

  • Supports file uploads

  • Not stored in browser history

Limitations of POST

  • Cannot be bookmarked

  • Slightly slower due to processing overhead


POST Method – Developer Example

HTML Form (register.html)

<form action="register.php" method="POST">
  <label>Name:</label>
  <input type="text" name="username">

  <label>Email:</label>
  <input type="email" name="email">

  <button type="submit">Submit</button>
</form>

PHP Processing (register.php)

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars($_POST['username']);
    $email = htmlspecialchars($_POST['email']);

    echo "Name: " . $name . "<br>";
    echo "Email: " . $email;
}
?>

GET vs POST: Key Differences

Feature GET POST
Data Visibility Visible in URL Hidden
Security Low High
Data Length Limit Yes No
Form Type Search, filter Login, registration
Bookmarkable URL Yes No
File Upload Support No Yes

When to Use Which?

Use GET when:

  • Search queries

  • Sorting or filtering

  • Non-sensitive data

  • Bookmarkable pages

Use POST when:

  • Login credentials

  • Password forms

  • Database write operations

  • File uploads

  • Sensitive or large data


Security Tips for GET and POST

  • Always use htmlspecialchars() to prevent XSS

  • Validate user input using filter_var()

  • Use HTTPS for secure applications

  • Never send passwords through GET

  • Hash passwords using password_hash()


Conclusion

Understanding the difference between PHP GET and POST methods is essential for building secure and functional web applications. GET is ideal for simple requests like searching, while POST is the preferred method for sending sensitive or large data.

Mastering these methods is the foundation for becoming a strong PHP developer.

Article