← Back to PHP Course | Chapter 2: Output & Input | Lesson 4 of 8

PHP $_GET & $_POST

GET and POST are two ways a form can send information. GET is like writing your message on a postcard anyone can read, while POST is like sealing it in an envelope.
Syntax
php
$_GET["key"];    // from the URL query string: page.php?key=value
$_POST["key"];   // from the request body of a submitted form

Understanding $_GET

$_GET collects values appended to the URL as a query string (?search=php), which means the data is visible in the address bar, browser history, and server logs.

That visibility is a feature for shareable, bookmarkable pages like search results — and a liability for anything sensitive.

Example: Understanding $_GET

php
<?php
// Set `$_GET['search']` to "php"
$_GET['search'] = "php";
// Print `"Search query: " . $_GET['search']` to the output
echo "Search query: " . $_GET['search'];
?>

Understanding $_POST

$_POST carries its data in the HTTP request body instead of the URL, so it never appears in the address bar or gets cached in browser history.

That's why login forms, payment forms, and anything else handling sensitive data almost always use POST rather than GET.

Example: Understanding $_POST

php
<?php
// Set `$_POST['password']` to "secret123"
$_POST['password'] = "secret123";
// Print "Password never appears in the URL: " . strlen($_POST['password']) . " chars" to the output
echo "Password never appears in the URL: " . strlen($_POST['password']) . " chars";
?>

When to Use $_GET vs $_POST

As a rule of thumb: use $_GET for requests that only *read* data and are safe to bookmark or reload (search filters, pagination).

Use $_POST for anything that *changes* server state — creating an account, placing an order — where an accidental page reload shouldn't silently repeat the action.

Example: When to Use $_GET vs $_POST

php
<?php
// $_GET for reading/searching -- safe to bookmark
$_GET['page'] = 2;
// $_POST for actions that change state
$_POST['create_account'] = true;
echo "GET page: " . $_GET['page'];
?>

Safe $_GET Parameters

Because $_GET values live in the URL, a user can edit them directly in the address bar before the request even reaches your server — ?id=5 becomes ?id=6 with zero effort.

Never assume a $_GET value is the one your own links generated; validate and sanitize it exactly as you would any other user input.

Example: Safe $_GET Parameters

php
<?php
$_GET['id'] = "6"; // user could edit this in the address bar
$id = (int) $_GET['id'];
echo "Validated id: " . $id;
?>

Processing POST Forms

Before acting on a $_POST submission, confirm $_SERVER[REQUEST_METHOD] === POST (or check that the expected keys are actually set).

Skipping this check means a script written to expect a form submission can misbehave — or throw warnings — on a plain page load.

Example: Processing POST Forms

php
<?php
// Set `$_SERVER['REQUEST_METHOD']` to 'POST'
$_SERVER['REQUEST_METHOD'] = 'POST';
// Check whether `$_SERVER['REQUEST_METHOD'] === 'POST'`
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Print "Processing the submitted form." to the output
    echo "Processing the submitted form.";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Sending passwords or other sensitive data with $_GET, which places them in the URL, browser history and server logs.
  2. Trusting $_GET[id] directly in a database query, which allows SQL injection; validate it and use prepared statements.
  3. Setting method="post" in the form but reading $_GET, or the reverse, so the expected array is empty.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.