Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 1 of 8

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.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

HTTP Request Anatomy & PHP's Role

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.

Diagram
Loading diagram…

Visual architecture and concepts for HTTP Request/Response Lifecycle

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.

Finished this lesson?

0 of 8 in Module 3
Sign in to save progress

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done