How the web works
What actually happens between typing an address and seeing a page: DNS, a connection, an HTTP request, a response with a status code, and a browser that puts it together. With real requests and responses from a tiny PHP server, cookies, forms, redirects, and why HTTP forgets you between requests.
Most code written today ends up behind a web page or an API. Frameworks hide a lot of what happens, which is useful until something goes wrong: a redirect loop, a form that “doesn’t save”, a cookie that disappears, a request that works in the browser and fails from the server. Then you need to know what’s underneath. This part is about that layer.
The good news: it’s mostly text going back and forth, and you can read all of it.
Clients, servers, and the trip in between
MDN’s beginner guide starts with one sentence: “Computers connected to the internet are called clients and servers.” Your browser is a client. The machine that hosts a website is a server. When you type an address, MDN lists roughly what happens:
- “The browser goes to the DNS server and finds the real address of the server that the website lives on.”
- “The browser sends an HTTP request message to the server, asking it to send a copy of the website to the client.”
- The server answers, ideally with “200 OK”, and sends the files.
- “The browser assembles the small chunks into a complete web page and displays it to you.”
Let’s go through the pieces.
DNS: names to addresses
Computers on a network find each other by IP addresses, numbers like 203.0.113.10 or longer IPv6 addresses. People prefer names like raqolbi.my.id. DNS connects the two. “DNS (Domain Name System) is a hierarchical and decentralized naming system for Internet-connected resources.”
You can ask it yourself: dig raqolbi.my.id or nslookup raqolbi.my.id shows the address the name currently points to. When you move a site to a new server, you change its DNS record, and resolvers around the world pick up the change over minutes to hours, depending on the record’s TTL (time to live). That’s why “I updated DNS but it still shows the old site” is so common.
HTTP: a conversation in plain text
“HTTP is a protocol for fetching resources such as HTML documents.” It’s “a client-server protocol, which means requests are initiated by the recipient, usually the Web browser.”
The easiest way to understand HTTP is to see it. Here’s a tiny PHP app running on PHP’s built-in server, and curl -v showing the conversation. Lines starting with > are the request; lines starting with < are the response:
> GET / HTTP/1.1
> Host: 127.0.0.1:8099
> User-Agent: curl/8.5.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Host: 127.0.0.1:8099
< Date: Sat, 07 Nov 2026 13:46:25 GMT
< Connection: close
< X-Powered-By: PHP/8.3.6
< Content-Type: text/html; charset=utf-8
<
<h1>Hello</h1>
Every request has the same shape:
- a method (
GET) and a path (/), - headers: name/value lines with extra information (
Host,User-Agent,Accept), - an empty line,
- and optionally a body.
Every response has the same shape too: a status line (200 OK), headers (Content-Type tells the browser this is HTML), an empty line, and the body.
It really is just text. A few lines of Python with a raw network socket, no HTTP library at all, get the same answer:
import socket
with socket.create_connection(("127.0.0.1", 8099)) as s:
s.sendall(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n")
print(s.recv(4096).decode())
HTTP/1.1 200 OK
Host: 127.0.0.1
...
Content-Type: text/html; charset=utf-8
<h1>Hello</h1>
Frameworks, fetch(), Guzzle, and requests all build and parse these messages for you. Knowing what they look like means you can read them when something breaks.
Methods: what the request wants
The method says what kind of action the client wants:
GETreads something. It shouldn’t change anything on the server, so browsers, caches, and crawlers feel free to repeat it.POSTsubmits something: a form, a new order, an upload. Repeating it might create a second order.PUT/PATCHreplace or update something;DELETEremoves it. These matter most for APIs (part 20).
The practical rule: never change data on a GET. A “delete” link that works with GET can be triggered by a prefetching browser, a link preview, or a crawler.
Status codes: how it went
The first digit of the status code tells you the category:
| Range | Meaning | Common examples |
|---|---|---|
1xx |
informational | rarely seen directly |
2xx |
success | 200 OK, 201 Created, 204 No Content |
3xx |
redirection | 301 Moved Permanently, 302 Found, 303 See Other, 304 Not Modified |
4xx |
client error: the request was wrong | 400 Bad Request, 401, 403 Forbidden, 404 Not Found |
5xx |
server error: the server failed | 500 Internal Server Error, 502 Bad Gateway, 503, 504 |
That split is useful when debugging (part 15). A 4xx says “look at what was sent”. A 5xx says “look at the server’s logs”.
From the same PHP app: a path that doesn’t exist returns 404, and posting an invalid quantity returns 400 with a clear message, following part 13:
$ curl -i -d 'qty=3x' http://127.0.0.1:8099/orders
HTTP/1.1 400 Bad Request
qty must be a whole number of at least 1
The same request from JavaScript, with fetch:
const res = await fetch("http://127.0.0.1:8099/orders", {
method: "POST",
body: new URLSearchParams({ qty: "3x" }),
});
console.log(res.status, res.statusText);
console.log(await res.text());
400 Bad Request
qty must be a whole number of at least 1
Note that fetch doesn’t throw on a 400 or 500; the request itself succeeded, and the status says how it went. Checking res.ok or res.status is your job.
Redirects
A 3xx response with a Location header tells the client to go somewhere else:
$ curl -i http://127.0.0.1:8099/old-page
HTTP/1.1 301 Moved Permanently
Location: /
301 means “moved for good”: browsers and search engines remember it. 302 and 303 are temporary.
A common pattern for forms is Post/Redirect/Get: after a successful POST, answer with a redirect instead of a page:
$ curl -i -d 'qty=2' http://127.0.0.1:8099/orders
HTTP/1.1 303 See Other
Location: /orders/42
The browser then does a GET /orders/42. If the user presses refresh, it repeats the harmless GET, not the POST that created the order.
HTTP forgets you: cookies
“HTTP is stateless: there is no link between two requests being successively carried out on the same connection.” Every request arrives on its own. The server doesn’t automatically know that this request comes from the same person who logged in a minute ago.
Cookies fill that gap: “while the core of HTTP itself is stateless, HTTP cookies allow the use of stateful sessions.” A cookie “is a small piece of data a server sends to a user’s web browser”. The server sends it in a Set-Cookie header, and the browser sends it back on later requests in a Cookie header.
The PHP app counts visits with a cookie:
$count = (int) ($_COOKIE['visits'] ?? 0) + 1;
setcookie('visits', (string) $count, ['httponly' => true, 'samesite' => 'Lax']);
echo "Visit number $count\n";
Visit number 1
Visit number 2
> Cookie: visits=2
< Set-Cookie: visits=3; HttpOnly; SameSite=Lax
The third request shows both directions: the client sends back visits=2, and the server sets visits=3.
Logins work the same way, with one important difference: the cookie holds only a random session ID, and the actual data (who you are, your cart) stays on the server. Anything in a cookie can be read and changed by the user, so never trust it as-is. HttpOnly keeps JavaScript from reading the cookie, and SameSite limits when it’s sent from other sites. Part 22 covers why that matters.
HTTPS: the same conversation, encrypted
Everything above travels as readable text. On a real network, anyone between you and the server (public Wi-Fi, an ISP, a compromised router) could read or change it. HTTPS wraps the same HTTP messages in TLS, which MDN describes as “a protocol used by applications to communicate securely across a network, preventing tampering with and eavesdropping on email, web browsing, messaging, and other protocols.”
The requests and responses inside are exactly the same. What changes is that they’re encrypted, and the browser checks the server’s certificate so it knows it’s talking to the real raqolbi.my.id. Today there’s no reason for any site to use plain HTTP; hosts like Vercel and free certificates from Let’s Encrypt make HTTPS the default.
Then the browser takes over
The first response is usually HTML. The browser reads it and finds more things to fetch: CSS, JavaScript, images, fonts. Each one is another HTTP request, often to other servers. Then it builds the page, applies styles, and runs scripts, which may make even more requests with fetch.
Open your browser’s DevTools and look at the Network tab while loading a page. Every row is a request like the ones above, with its method, status, headers, size, and timing. It’s the single most useful view for understanding a web app, and the first place to look when a page is slow or broken.
Where your code fits
On the server, a framework (Laravel, Django, Express, Go’s net/http) receives the request, picks the code to run based on the method and path (routing), and turns your return value into a response. PHP’s built-in server used here is handy for that loop on your own machine. The PHP manual is clear about its limits: “This web server is designed to aid application development.” and “It should not be used on a public network.” Production uses a real web server or a platform in front (part 23).
Why this matters day to day
- Debugging web apps starts with the Network tab: method, status, headers, body. Most “it doesn’t work” reports become clear there.
- 4xx vs 5xx tells you whose side the problem is on before you open any code.
- Forms and refresh behave correctly once you use
POSTfor changes and redirect after success. - Sessions and login bugs are usually cookie bugs: wrong domain, missing
Secureon HTTPS,SameSiteblocking a cross-site request. - AI-generated web code often gets methods and status codes wrong in small ways, like returning
200with an error message in the body. You can spot that now.
Try it yourself
- Run
curl -v https://example.comand identify the request line, request headers, status line, and response headers. - Open DevTools → Network on a site you use. Find one
GET, onePOST, one redirect, and one request that sets a cookie. - Run PHP’s built-in server (
php -S localhost:8000) with a small script that returns a404for unknown paths and a303after a formPOST. - Use
digornslookupon a domain you own. What does it point to?
Summary
- The web is clients and servers exchanging HTTP messages, which are plain text: method, path, headers, body; status, headers, body.
- DNS turns names into addresses; changes take time to spread.
- Methods state intent:
GETreads,POSTsubmits. Don’t change data onGET. - Status codes say how it went:
2xxok,3xxgo elsewhere,4xxthe client’s fault,5xxthe server’s. - Redirects use
Location; Post/Redirect/Get keeps refresh safe. - HTTP is stateless; cookies carry state between requests. Never trust their contents.
- HTTPS is the same conversation inside TLS.
- The browser’s Network tab shows all of it.
References
- How the web works, An overview of HTTP, and Using HTTP cookies, MDN Web Docs.
- DNS and TLS, MDN Glossary.
- RFC 9110: HTTP Semantics, IETF.
- Built-in web server, PHP manual.