Unit 10 · Back-End Programming

10.2HTTP Request Methods: GET, POST etc.

MethodPurposeData locationVisible in URL?
GETRetrieve a page or dataQuery string: page.php?id=5&cat=booksYes
POSTSend data to create or process somethingRequest bodyNo
PUT / PATCHReplace / partially update a resource (APIs)BodyNo
DELETEDelete a resource (APIs)URLYes
HEAD / OPTIONSHeaders only / ask what is allowed
GETshop.lk/search.php?q=pen&page=2query string: name=value pairsvisible, bookmarkable,saved in history and logsUse for: search, filters, pages,viewing a product (read-only)POSTPOST /login.php HTTP/1.1Host: shop.lkContent-Type: application/x-www- form-urlencodedemail=nimal%40x.com&password=…data in the request body, not the URLUse for: login, register, checkout, uploads(anything that changes data); always over HTTPS
GET versus POST: where the form data travels

When to use GET versus POST

Handling a form in PHP. register.html:

HTML
<form action='register.php' method='post'>
  <input type='text' name='name' required>
  <input type='email' name='email' required>
  <input type='password' name='password' required>
  <button type='submit'>Register</button>
</form>

register.php:

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    exit('Method not allowed');
}

$name  = trim($_POST['name'] ?? '');       // ?? gives a default if the key is missing
$email = trim($_POST['email'] ?? '');
$pass  = $_POST['password'] ?? '';

// SERVER-SIDE VALIDATION (never trust the browser)
$errors = [];
if (strlen($name) < 3)                          $errors[] = 'Name too short';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Invalid email';
if (strlen($pass) < 8)                          $errors[] = 'Password must be 8+ characters';

if ($errors) {
    foreach ($errors as $e) { echo '<p>' . htmlspecialchars($e) . '</p>'; }
    exit;
}

echo 'Welcome, ' . htmlspecialchars($name);

Superglobals (built-in arrays available everywhere)

VariableContains
$_GETValues from the URL query string
$_POSTValues from a POST body
$_REQUESTGET + POST + cookies (avoid; be explicit)
$_SERVERServer and request info (REQUEST_METHOD, REQUEST_URI)
$_COOKIECookies sent by the browser
$_SESSIONSession data stored on the server
$_FILESUploaded files

File upload sketch:

PHP
if (isset($_FILES['photo']) && $_FILES['photo']['error'] === 0) {
    $allowed = ['image/jpeg', 'image/png'];
    $type = mime_content_type($_FILES['photo']['tmp_name']);
    if (in_array($type, $allowed) && $_FILES['photo']['size'] < 2000000) {
        $ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
        $newName = uniqid('img_') . '.' . $ext;                  // never trust the original name
        move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/' . $newName);
    }
}

The form needs method='post' enctype='multipart/form-data'.

Redirects: header('Location: success.php'); exit; (call before any output). The Post/Redirect/Get pattern (redirect after a successful POST) stops a refresh from re-submitting the form.

Try it: Build a contact form that validates on the server, displays errors next to the form (re-showing what the user typed), and redirects to a thank-you page on success. Add a search page that uses GET (search.php?q=pen).