Web security · Lesson 1 of 2

How websites work

Clients, servers, HTML, JavaScript, databases, cookies and sessions explained for beginners, so web security finally makes sense.

⏱ 15 min🎯 Beginner🧪 Hands-on✅ 4-question quiz🎬 Video✓ Completed

By the end, you’ll be able to…

  • Describe what happens between your browser and a web server
  • Explain the front end, back end and database of a web app
  • Explain how cookies keep you logged in, and why stolen cookies are dangerous

Key words

Client
The program asking for something. Usually your browser.
Server
The computer that answers requests and sends back pages or data.
Front end
What runs in your browser: HTML (structure), CSS (style) and JavaScript (behaviour).
Back end
Code running on the server that handles logins, saves data and talks to the database.
Cookie
A small piece of data a website asks your browser to store and send back with later requests.
Session
The website remembering that you’re logged in, usually through a session cookie.

See all 127 words in the glossary →

A website is a conversation

Every time you open a page, your browser (the client) sends a request to a server, and the server sends back a response. A single page can involve dozens of these: one for the page, more for images, fonts, scripts and data.

  1. BrowserGET /profile
  2. Request+ cookie: session=…
  3. Server codewho is this? are they allowed?
  4. Databasefind this user’s profile
  5. Response200 OK + HTML
Every page load is a request and a response. Web security is mostly about what happens in the middle.

You met HTTP in HTTP, HTTPS & TLS. This lesson zooms in on what the website does with your request.

Three layers of a web app

  1. Front end (your browser)HTML = structure, CSS = style, JavaScript = behaviour
  2. Back end (the server)checks logins and permissions, runs the app’s rules
  3. Databasestores users, posts, orders, scores
The golden rule of web security: the back end must never trust anything the front end sends.

Here’s the key idea: you control the front end. Anything running in your browser can be viewed and changed by you, using the browser’s developer tools. That’s why the back end must check everything again: prices, permissions, user IDs, everything.

Cookies and sessions: how you stay logged in

HTTP has no memory. Each request arrives on its own. So how does a site remember you’re logged in?

  1. You log inusername + password
  2. Server makes a sessionrandom ID: 7f3a…
  3. Set-Cookiebrowser stores it
  4. Every requestcookie sent back automatically
  5. Server recognises youuntil you log out or it expires
The session cookie works like a wristband at a festival. Show it and you’re let in, no password needed.

That wristband idea explains a big modern threat: if malware or an attacker steals your session cookie, they can sometimes use your account without your password or MFA code. That’s what infostealer malware goes after. Logging out of important sites, and the sites using protections like HttpOnly and Secure cookie settings, makes this harder.

Where JavaScript fits

JavaScript makes pages interactive: menus, games, live chat, this site’s practice terminal. Browsers run it in a sandbox with strict rules. A key one is the same-origin policy: a script from one website normally can’t read your data on another website. Many web attacks are about sneaking script into the wrong site, so it runs with that site’s permissions. You’ll meet that in the next lesson.

Look under the hood (safely)

Your own browser can show you all of this, on any site you visit:

  1. Press F12 (or right-click → Inspect) to open developer tools.
  2. Open the Network tab and reload the page. Every row is a request.
  3. Click one to see its headers: the method, status code, and any cookies sent.
  4. Open Application (Chrome) or Storage (Firefox) to see this site’s cookies.

Looking is fine. Changing requests to break into someone else’s account isn’t: that’s what legal labs like PortSwigger’s Web Security Academy are for.

🤔 Something not clicking?

🧪 Hands-on

Practice

Use curl (a command-line web client) to talk to a pretend web server. Everything here is simulated.

🎬 Watch

Video: The Internet: HTTP & HTML

WebThe Internet: HTTP & HTMLCode.org · Watch on YouTube

Video by Code.org, hosted on YouTube. It loads only when you press play.

✅ Check yourself

Quick quiz

Every answer comes with an explanation, right or wrong. Your best score is saved in this browser.

Q1. Which part of a web app should decide whether you’re allowed to see a page?
Q2. What does a session cookie usually do?
Q3. Why is a stolen session cookie dangerous?
Q4. You change a price in your browser’s developer tools from $50 to $1. A well-built shop should…

What’s next

Up nextCommon web vulnerabilities →Broken access control, SQL injection and cross-site scripting explained for beginners, with a safe in-browser demo and how developers fix each one.⏱ 18 min