SyntaxStudy
Sign Up
PHP Beginner 5 min read

GET vs POST Methods

HTML forms can submit data using either the GET or POST HTTP method. Understanding the difference is fundamental to writing correct and secure web applications.

  • GET: Data is appended to the URL as a query string. Bookmarkable, cacheable, but limited in size and visible in browser history. Use for search forms and filters.
  • POST: Data is sent in the request body. Not cached, not stored in history, suitable for large payloads. Use for login, registration, and any state-changing action.
Example
<?php
// Access GET parameters ($_GET superglobal)
// URL: /search.php?query=php&page=2
$query = $_GET['query'] ?? '';
$page  = (int) ($_GET['page'] ?? 1);

echo "Searching for: " . htmlspecialchars($query);
echo "Page: $page";

// Access POST parameters ($_POST superglobal)
// Submitted from <form method="post">
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    echo "Login attempt for: " . htmlspecialchars($username);
}

// $_REQUEST merges GET, POST, and COOKIE — avoid it
// It can be a security issue since origin is ambiguous

// Check which method was used
$method = $_SERVER['REQUEST_METHOD']; // "GET" or "POST"
echo "Method: $method";
Pro Tip

Tip: Never use GET for actions that modify data (deletes, updates, payments). A search engine crawler or a pre-fetching browser could trigger destructive operations by following GET links.