LearnContact
Lesson 3220 min read

GET & POST Methods

Learn the difference between the GET and POST HTTP methods and when to use each one when handling PHP forms.

Introduction

In the previous two lessons you learned how to handle form submissions and validate the data a visitor sends. But every HTML form also has to decide how it sends that data to the server. That decision is made with the form's method attribute, and PHP gives you two matching superglobals to read the result: $_GET and $_POST.

This lesson focuses specifically on the difference between the GET and POST HTTP methods, how each one carries data from the browser to the server, and how to decide which one a given form should use.

What You Will Learn
  • What GET and POST are and where each one puts the submitted data.
  • How to read GET data with $_GET and POST data with $_POST.
  • The practical trade-offs between the two methods.
  • How to pick the right method for a search form vs. a login form.
  • How the method attribute on a <form> tag controls all of this.

What is an HTTP Method?

Every time a browser talks to a web server, it sends an HTTP request, and that request has a "method" describing what kind of action it represents. GET and POST are by far the two most common methods used with HTML forms, and PHP automatically separates their data into two different superglobal arrays.

Quick Definition
  • GET requests ask the server for data, and any form data rides along in the URL.
  • POST requests send data in the body of the request, hidden from the URL.
  • PHP reads GET data into $_GET and POST data into $_POST.

The GET Method

With the GET method, form data is appended directly to the URL as a query string — the part after the ? symbol, with each field joined by &. Because the data lives in the URL, it is visible to the visitor, can be bookmarked, and can be shared as a link.

search-form.html
<form action="search.php" method="get">
    <input type="text" name="query" placeholder="Search...">
    <button type="submit">Search</button>
</form>

Submitting "php tutorials" produces a URL like search.php?query=php+tutorials. On the receiving page, that value is available in the $_GET superglobal.

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

Visible in the URL

Every field and value can be seen (and edited) directly in the address bar.

Bookmarkable

Because the data is part of the URL, the exact result can be bookmarked or shared.

Size-Limited

Most browsers and servers cap URL length at roughly 2000 characters.

Not for Secrets

Never send passwords or sensitive data with GET — it ends up in browser history and server logs.

The POST Method

With the POST method, form data is sent in the body of the HTTP request instead of the URL. The visitor never sees the submitted values in the address bar, there is no practical size limit, and POST is the standard choice for anything sensitive or large.

login-form.html
<form action="login.php" method="post">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Log In</button>
</form>
login.php
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $username = $_POST["username"];
    // Never echo a raw password back to the page!
    echo "Welcome, " . htmlspecialchars($username) . "!";
}
?>
Example Output
Welcome, rahul!

Hidden from the URL

Submitted values never appear in the address bar, browser history, or bookmarks.

No Practical Size Limit

POST can carry large payloads — long text, multiple fields, even files.

Better for Sensitive Data

Passwords, personal details, and payment information should always use POST.

Not Bookmarkable

Because the data lives in the request body, a POST result cannot be bookmarked as a URL.

GET vs POST

GET

  • Data appended to the URL as a query string
  • Read with $_GET
  • Limited to a few thousand characters
  • Bookmarkable and shareable as a link
  • Never use for passwords or sensitive data

POST

  • Data sent in the request body
  • Read with $_POST
  • No practical size limit
  • Not bookmarkable — the data is not in the URL
  • Standard choice for logins, sensitive or large submissions

Choosing the Right Method

The right method depends on what the form does, not personal preference. A useful rule of thumb: if the result should be bookmarkable and the data is not sensitive, use GET. If the form changes something on the server or handles sensitive or large data, use POST.

ScenarioRecommended MethodWhy
Search boxGETLets visitors bookmark or share the search results URL.
Login formPOSTCredentials must never appear in the URL or browser history.
Filtering a product listGETFilter choices are useful to bookmark and share.
Submitting a large comment or filePOSTGET has a practical size limit that a long submission could exceed.
Changing account settingsPOSTThe action modifies data on the server rather than just requesting it.

Example: A Search Form vs a Login Form

Putting it together, here is why a search form typically uses GET while a login form uses POST — both use the exact same PHP superglobal pattern, just with different data flowing through it.

both.php
<?php
// Search form (GET) — safe to expose in the URL, nice to bookmark
if (isset($_GET["query"])) {
    echo "Searching for: " . htmlspecialchars($_GET["query"]) . "\n";
}

// Login form (POST) — must stay out of the URL and browser history
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["username"])) {
    echo "Login attempt for: " . htmlspecialchars($_POST["username"]) . "\n";
}
?>
Example Output
Searching for: laptop
Login attempt for: rahul

Common Mistakes

Avoid These Mistakes
  • Submitting passwords or personal data with method="get" — they end up visible in the URL and server logs.
  • Forgetting that GET data always shows up in $_GET, never $_POST, and vice versa.
  • Assuming GET data is safe just because it is "only in the URL" — it can still be tampered with, so always validate it.
  • Using GET for actions that change data on the server, like deleting a record from a single link click.

Best Practices

  • Use GET for read-only requests that a visitor might want to bookmark or share, like searches and filters.
  • Use POST for anything sensitive, large, or that changes data on the server, like logins and account updates.
  • Always check isset() before reading a $_GET or $_POST value to avoid warnings.
  • Always escape output with htmlspecialchars() before echoing user-submitted data back to the page.
  • Check $_SERVER["REQUEST_METHOD"] when a script needs to confirm exactly which method was used.

Frequently Asked Questions

Can a single form use both GET and POST?

A form itself only submits with one method at a time, but a page can certainly read from both $_GET and $_POST — for example, a URL parameter combined with a posted form.

Is data sent with POST completely hidden and secure?

No. POST data is not visible in the URL, but it is still sent in plain text unless the site uses HTTPS. Always use HTTPS for anything sensitive, regardless of method.

What happens if I submit a GET form without any query string?

PHP simply has no keys in $_GET, so isset($_GET["field"]) returns false and your code should handle that case gracefully.

Is there a maximum size for POST data?

PHP's configuration (post_max_size in php.ini) sets a practical limit, but it is far larger than what a URL can hold with GET.

Why does my browser warn me about resubmitting a form?

That warning appears after a POST request when you refresh or go back, because the browser would need to resend the same data — a side effect of POST not being idempotent like GET.

Key Takeaways

  • GET sends form data in the URL query string; POST sends it in the request body.
  • PHP reads GET data through $_GET and POST data through $_POST.
  • GET is bookmarkable and size-limited; POST is not bookmarkable but has no practical size limit.
  • Never use GET for passwords or other sensitive data.
  • The method attribute on a <form> tag determines which superglobal receives the data.

Summary

GET and POST are the two workhorse HTTP methods behind almost every HTML form. GET keeps data visible in the URL and is ideal for bookmarkable, read-only requests like searches, while POST hides data in the request body and is the right choice for sensitive or large submissions like logins.

In this lesson, you learned how each method carries data, how to read it with $_GET and $_POST, and how to decide between them. Next, you will build on this by handling file uploads — a special case that always requires POST.

Lesson 32 Completed
  • You understand the difference between GET and POST.
  • You can read submitted data with $_GET and $_POST.
  • You know when to choose one method over the other.
  • You are ready to handle file uploads with PHP.
Next Lesson →

File Upload