GET and POST are HTTP methods that allow sending data from a form to the server.
For a user to send data to your PHP script (a name, a password, a search), we use HTML forms. The key lies in the method attribute of the <form> tag. Depending on which you choose (GET or POST), PHP will receive the data in a different ‘inbox’ (array).
The GET method ($_GET)
When you use method="GET", the data travels publicly in the URL.
- What does it look like?
yourwebsite.com/search.php?product=sneakers&size=42 - How to retrieve it in PHP? Using the superglobal array
$_GET.
Characteristics:
- Visible: Everyone sees the data in the address bar.
- History: The URL (with the data) is saved in the browser history.
- Shareable: You can copy the URL and send it to a friend (ideal for search results).
- Limit: It has a character limit (around 2000, depending on the browser).
When to use it? To request information without modifying anything on the server: Search engines, product filters, pagination (page 1, page 2…).
// URL: profile.php?name=Ana&age=25
$name = $_GET['name']; // "Ana"
$age = $_GET['age']; // "25"
echo "Hello $name, you are $age years old.";The POST method ($_POST)
When you use method="POST", the data travels hidden within the body of the HTTP request. They are not visible in the URL.
- What does it look like?
yourwebsite.com/login.php(The URL remains clean). - How to retrieve it in PHP? Using the superglobal array
$_POST.
Characteristics:
- Invisible: The data does not appear in the address bar (although a technical user can see it by inspecting the browser’s network traffic).
- Greater capacity: The server imposes limits through options like
post_max_size; it is not unlimited. - Does not appear in the URL: It is suitable for credentials, but only HTTPS encrypts the request during transport.
When to use it? To send information that will be processed, stored, or will modify something: Registration forms, Login, posting a comment, changing settings.
// HTML: <form method="POST"> <input name="email"> ...
$email = $_POST['email'];
// The user does not see the email in the URLComparison between GET and POST
| Feature | GET | POST |
|---|---|---|
| Superglobal | $_GET | $_POST |
| Visibility | Visible in URL | Hidden in the body |
| Sensitive data | Do not use for passwords | Use together with HTTPS and validation |
| Ideal Use | Searches, Filters | Inserts, Deletions, Updates, Login |
| Bookmarks | Can be bookmarked | Cannot be bookmarked |
Integrated practical example
It is very common to have the form and the PHP code that processes it in the same file. We use a condition to know if it has been submitted.
<?php
// We check if the form was submitted via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// We retrieve the data from the input with name="user"
$user = $_POST['user'];
echo "<div style='background:green; color:white; padding:10px;'>Received: Hello $user</div>";
}
?>
<form method="POST" action="">
<label for="name">Your name:</label>
<input type="text" id="name" name="user" placeholder="Write here...">
<button type="submit">Send my data</button>
</form>In this example we are directly printing what the user writes ($user). If the user enters malicious Javascript code (<script>alert('hack')</script>), our site will execute it. This is a serious vulnerability called XSS.