HTTP Request/Response Lifecycle
Understanding the HTTP lifecycle is the foundation of all web development. Every PHP script runs in response to an HTTP request — knowing what information arrives with that request and what you can send back is essential for building any web application.
HTTP Request Anatomy & PHP's Role
<?php
declare(strict_types=1);
// ── HTTP Request structure ─────────────────────────────────────
// GET /posts/hello-world HTTP/1.1
// Host: example.com
// Accept: text/html
// Cookie: session_id=abc123
// User-Agent: Mozilla/5.0...
//
// (Optional body for POST/PUT — not present in GET)
// ── PHP exposes the request via superglobals ───────────────────
// $PHP_SELF or better:
$method = $_SERVER["REQUEST_METHOD"]; // "GET", "POST", "PUT", "DELETE"
$uri = $_SERVER["REQUEST_URI"]; // "/posts/hello-world?page=2"
$host = $_SERVER["HTTP_HOST"]; // "example.com"
$ip = $_SERVER["REMOTE_ADDR"]; // "203.0.113.1" (client IP)
$agent = $_SERVER["HTTP_USER_AGENT"]; // browser/app identifier
$https = $_SERVER["HTTPS"] ?? "off"; // "on" if HTTPS
$referer = $_SERVER["HTTP_REFERER"] ?? ""; // where the user came from
// Parse the URI
$parsedUri = parse_url($_SERVER["REQUEST_URI"]);
$path = $parsedUri["path"]; // "/posts/hello-world"
$queryStr = $parsedUri["query"] ?? ""; // "page=2"
// ── HTTP Response — we CONTROL the output ─────────────────────
// Set status code
http_response_code(200); // 200 OK (default)
http_response_code(404); // Not Found
http_response_code(302); // Found (redirect)
// Set headers (must be before any output)
header("Content-Type: application/json; charset=UTF-8");
header("Cache-Control: no-store, no-cache");
header("X-Content-Type-Options: nosniff");
header("X-Frame-Options: DENY");
// Redirect
header("Location: /login");
exit; // ALWAYS exit after redirect — PHP keeps running otherwise!
// ── Request methods and their semantics ──────────────────────
// GET — Retrieve data. Safe + idempotent. No body.
// POST — Create/submit data. Not idempotent. Has body.
// PUT — Replace resource. Idempotent. Has body.
// PATCH — Partial update. Has body.
// DELETE — Remove resource. Idempotent.
// HEAD — Same as GET but no body in response (check headers only)Quick Quiz
Tip
Tip
Practice HTTP RequestResponse Lifecycle in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
PHP processes each request through the server-side engine
Practice Task
Note
Practice Task — (1) Write a working example of HTTP RequestResponse Lifecycle from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with HTTP RequestResponse Lifecycle is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.