PHP GET and POST Methods
PHP GET and POST method example for handling HTML forms
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.
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 |
The GET method appends form data directly in the URL as query parameters.
https://example.com/search.php?query=php+tutorial
Search forms
Filters & sorting
Pagination (page=1, page=2, …)
Passing small & non-sensitive data
Simple and easy to test
Bookmarkable URLs
Can be cached by browser
Data visible in the URL
Not secure for sensitive information
URL length limit
<form action="search.php" method="GET">
<label>Search:</label>
<input type="text" name="query">
<button type="submit">Search</button>
</form>
<?php
if (isset($_GET['query'])) {
$search = htmlspecialchars($_GET['query']);
echo "You searched for: " . $search;
}
?>
The POST method sends data securely inside the request body — NOT visible in the URL.
Login forms
User registration
Payment forms
File uploads
Storing sensitive data
More secure
Can send large amounts of data
Supports file uploads
Not stored in browser history
Cannot be bookmarked
Slightly slower due to processing overhead
<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
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['username']);
$email = htmlspecialchars($_POST['email']);
echo "Name: " . $name . "<br>";
echo "Email: " . $email;
}
?>
| 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 |
Search queries
Sorting or filtering
Non-sensitive data
Bookmarkable pages
Login credentials
Password forms
Database write operations
File uploads
Sensitive or large data
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()
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.