Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Dynamic pages, forms and cookies

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

55 minutes

Why does the page know your name?

You open a shop site. It says "Welcome back, Aisha". Your friend opens the same site. It says "Welcome back, Sam".

It is the same address. But the page is different. Somebody wrote code that builds each page when you ask for it.

How does the site remember you? That is today's puzzle.

Key words

WordWhat it meansExample
Dynamic pageA page that the server builds each time, from data.Welcome back, Aisha
Static pageA page that is the same file for everybody.
FormA group of boxes on a page where the user types data and presses Send.
Query stringData added to the end of an address, after a question mark.?name=Sam&age=15
StatelessDoes not remember anything from one request to the next.
StatefulRemembers what happened before.
SessionThe server's memory of one user, kept between requests.
CookieA small piece of text the server gives to the browser. The browser sends it back each time.
Front endThe part of a program that the user sees, in the browser.
Back endThe part of a program that runs on the server.
HTTPThe rules a browser and a server use to talk to each other.
EscapeChange special signs, such as <, so the browser shows them as text and does not run them.
ValidationChecking that data is sensible before you use it.
HttpOnlyA cookie setting. JavaScript on the page cannot read this cookie, so it is safer.

The cells students fill in are left blank in this preview.

Static and dynamic pages

A static page is a file. Everybody gets the same one.

A dynamic page is built when somebody asks. The server looks up data, such as your name or your basket, and puts it into the HTML.

Try it: build HTML from data

Code: python
from html import escape

items = ["Tea", "Milk", "Biscuits"]

rows = ""
for item in items:
    rows = rows + "<li>" + escape(item) + "</li>"

print("<ul>" + rows + "</ul>")

# Add an item that has a < sign in it. See what escape() does.

Always escape user text

data-callouts.warning.label

If a user types <script> into a box and you put it straight into the page, their code runs in other people's browsers. Use escape() on all text from users. It turns < into &lt;, so it shows as text and does not run.

Quick check

Have a go

Quick check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Forms send data

A form sends what the user typed to the server. The data arrives as text, like name=Sam&age=15.

The back end must split the text into parts and then validate it. Never trust a form. A user can send anything, even if your page has checks.

Try it: read form data

Code: python
from urllib.parse import parse_qs

body = "name=Sam&age=15"
data = parse_qs(body)
print(data)
print(data["name"][0])

# Each value is a list. Change age to abc. Can you check it is a number?
print(data["age"][0].isdigit())

The web forgets

The web is stateless. Each request is new. The server does not know that you were here a second ago.

To remember you, the server gives your browser a cookie. The browser sends it back with every request. The server reads it and finds your session, its memory of you.

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.