NightFox

NightFox

Web Programming Fundamentals

Every PHP app lives inside the HTTP request-and-response cycle. A request comes in, your code runs, a response goes out, and PHP forgets everything. Once that really sinks in, routing, forms, logins, APIs, caching, and debugging all make a lot more sense.

The request cycle

  1. The browser resolves the host. DNS returns the address of the web server or reverse proxy.
  2. A connection is established. HTTPS adds TLS negotiation before HTTP data is exchanged.
  3. The browser sends a request. The request includes a method, path, headers, and sometimes a body.
  4. The web server invokes PHP. Apache, nginx with PHP-FPM, or another server passes the request to the application.
  5. The application runs. It validates input, loads data, applies business rules, and produces a response.
  6. The browser receives the response. It uses the status code, headers, and body to decide what to display or do next.
GET /customers/42 HTTP/1.1
Host: example.test
Accept: text/html
Cookie: session_id=abc123

HTTP methods mean something

MethodTypical purposeExpected behavior
GETRead a page or resourceShould not create, modify, or delete data.
POSTCreate or submit an actionUsually has a request body and may change state.
PUTReplace a resourceCommon in APIs and generally idempotent.
PATCHPartially update a resourceChanges only specified fields.
DELETERemove a resourceShould require authorization and CSRF protection in browser applications.
Never use a plain GET link to delete something. Crawlers, browser prefetching, and accidental clicks all follow links. There are famous stories of search engine bots "clicking" every delete link in an admin panel. Use POST (or DELETE) and check authorization.

Status codes are part of the interface

CodeUse
200 OKA normal successful response.
201 CreatedA resource was created successfully.
204 No ContentThe action succeeded and no body is needed.
302 Found / 303 See OtherRedirect after an action, often following a successful form POST.
400 Bad RequestThe request is malformed.
401 UnauthorizedAuthentication is required or invalid.
403 ForbiddenThe identity is known but lacks permission.
404 Not FoundThe requested resource does not exist or should not be revealed.
422 Unprocessable ContentThe request is structurally valid but fails validation.
500 Internal Server ErrorAn unexpected application failure occurred.

Headers, content types, and cookies

Headers are the metadata riding along with every request and response. The ones I end up dealing with most are Accept, Content-Type, authorization headers, caching headers, and the forwarding headers a reverse proxy adds. (Those last ones have their own page.)

<?php
declare(strict_types=1);

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');

http_response_code(200);
echo json_encode(
    ['status' => 'ok'],
    JSON_THROW_ON_ERROR
);

HTTP doesn't remember you

Every request stands on its own. Sessions fake continuity by keeping data on the server and tying it to an ID in a cookie. And since the browser controls everything it sends, never base authentication on a hidden form field or a query string value. Anyone can change those.

Know what you can trust (not much)

  • Everything from $_GET, $_POST, cookies, JSON bodies, and uploaded files is untrusted.
  • Headers can be forged unless a trusted reverse proxy replaces or validates them.
  • Database values may still require escaping when rendered into HTML.
  • Authorization must be checked on every protected action, not only when the menu is displayed.
  • Validation answers “is this input acceptable?” while escaping answers “is this output safe in this context?”

Last Modified: October 12, 2026, 11:46 am