Four tasks that set up your shop, about 3.5 hours
These tasks set up the shop you work on for the rest of the course, so do them all and in order. You need the computer you will use for the course, the lesson's listings and a notes file to paste your results into. Everything runs on your own computer at localhost.
Task 1: Install PHP and serve a first page
- Install PHP 8.5 (or 8.4) as section 2 describes for your system.
- Run
php -v,php -mandphp --ini, and note the version, whetherPDOandpdo_sqliteare listed, and whichphp.iniis loaded. - Make a folder called
hellooutside your shop folder. In your editor, createhello/index.phpholding one line:<?php echo 'Hello from PHP ' . PHP_VERSION; - From the folder that holds
hello, runphp -S localhost:8000 -t helloand openhttp://localhost:8000. - Leave it running and, in a second terminal, run the same command again. Note the message. Close the second terminal.
- Stop the first server with Ctrl+C, start it with
-tnaming a folder that exists but has noindex.phpin it, open the page, and note what you see. Stop it again.
What you produce: a note with your PHP version, the two extension lines, the php.ini path, the text of the hello page, and the two messages from steps 5 and 6 with what each means and how you would fix it.
How to check: the hello page shows a version of 8.4 or later, and pdo_sqlite is in the extension list.
Time: 40 minutes.
Task 2: Build and seed the skeleton
- Make the folders and create the eleven files from section 4 exactly as listed.
- Run
php tools/setup.phpfrompractice-shop, then start the shop withphp -S localhost:8000 -t public. - Open the shop. Add the Stoneware Mug twice and the Linen Apron once, then remove the apron.
- Run
php tools/setup.phpa second time and note what it says, and why.
What you produce: the running shop at http://localhost:8000 with the five sample products and a working cart, your file tree, and the setup script's output from both runs.
How to check: the cart shows Stoneware Mug, quantity 2, R570.00, total R570.00, and the header says "Cart (2)".
Time: 1 hour 30 minutes.
Task 3: Tour the shop and record a baseline
- Open the developer tools, select Network, and turn on Persist Logs or Preserve log.
- In one sitting: load the product list, open Ouma's Rusk Tin, press "Add to cart", and look at the cart. Then load
/?r=product&id=999and/?r=nothing. - For each request, record the method, the address, the status, and the response headers
Content-Type,Set-Cookie,LocationandX-Powered-Bywhere they appear. - In Storage or Application, record the session cookie's name, domain, path, expiry, HttpOnly, Secure and SameSite.
- Write down which of these response headers the shop does not send yet:
Content-Security-Policy,X-Content-Type-Options,X-Frame-Options,Referrer-Policy,Strict-Transport-Security. - For each page you visited, write one line on what it does, and list the parts a customer or Jason would expect that are not built yet, with the module that builds each.
What you produce: a baseline record (a table of the requests, the cookie's settings and the headers not yet sent) and a short tour note, both kept in your notes file for Modules 4 and 8.
How to check: your table has the POST with status 303 and a Location header, only one response carries Set-Cookie, and both refusals have status 404.
Time: 50 minutes.
Task 4: Run the first checks and add a positive check of your own
- Create
tools/checks.phpfrom section 7. With the shop running, runphp tools/checks.phpin a second terminal. - Stop the shop and run the checks again. Note the message and start the shop again.
- By hand: open the rusk tin's page and view its source. Find the heading as the shop sent it, and write two sentences on what you see and what it shows about the shop.
- Turn that into a check. Add a fourth check to
checks.php, above the line// Each later module adds its checks above this line., that passes when the rusk tin's page has the name in its heading in encoded form. Run the checks again.
What you produce: the output of the first run (three PASS lines), the message from step 2, your two sentences, your new check's code, and the output of the last run (four PASS lines).
How to check: the last run ends "All checks passed." and your check's name says what a working control does, not how anything could be attacked.
Time: 40 minutes.
Show the model answer
Task 1: Install PHP and serve a first page
The installation, by system.
| System | What you run or do |
|---|---|
| Windows | Install the Visual C++ Redistributable if needed, unzip the VS17 x64 Non Thread Safe Zip into C:\php, add C:\php to your account's Path, copy php.ini-development to php.ini and remove the leading character from ;extension_dir = "ext" and ;extension=pdo_sqlite, then open a new PowerShell window. |
| macOS | Install Homebrew from https://brew.sh, then brew install php@8.5 and brew link --force --overwrite php@8.5. |
| Ubuntu 26.04, Debian 13 | sudo apt install php-cli php-sqlite3 |
| Ubuntu 24.04, Linux Mint 22 | Add ppa:ondrej/php as php.net's download page shows, then sudo apt install php8.5-cli php8.5-sqlite3. |
A good note looks like this, from a Linux machine (yours differs in the version's build date and the paths):
$ php -v
PHP 8.5.12 (cli) (built: ...) (NTS)
$ php -m
...
PDO
pdo_sqlite
...
$ php --ini
Loaded Configuration File: /etc/php/8.5/cli/php.ini
Hello page: "Hello from PHP 8.5.12".
Step 5, second server on the same port: Failed to listen on localhost:8000 (reason: Address already in use). Only one program can listen on a port. Fix: stop the other server with Ctrl+C, or start this one on another port, such as 8080.
Step 6, a folder with no index.php: a plain grey "Not Found" page, "The requested resource / was not found on this server", with status 404. The server looked for an index file in the folder I named with -t and found none. Fix: start the server from the right folder, naming the right document root. For the shop that is always -t public from inside practice-shop.
On Windows, php --ini shows Loaded Configuration File: C:\php\php.ini. If it says (none), PHP did not find your php.ini: check that the file is in the same folder as php.exe and is not named php.ini.txt, which Windows hides by default. If pdo_sqlite is missing on Windows, the two lines in php.ini still start with the character you were to delete. If a command says php is not recognised, the Path change has not reached that window: open a new one.
Task 2: Build and seed the skeleton
The file tree after the setup script has run:
practice-shop/
data/
shop.sqlite
migrations/
000_products.sql
public/
index.php
style.css
src/
config.php
db.php
helpers.php
security.php
pages/
cart.php
catalogue.php
product.php
tools/
setup.php
(tools/checks.php joins it in Task 4.)
The setup output. The first run says Applied 000_products.sql and then The database is ready: with the database's path. The second run says only The database is ready:, because the migrations bookkeeping table already records 000_products.sql as applied, so the script skips it rather than creating the table and the sample products a second time.
It works when:
- The product list shows five products in alphabetical order: Linen Apron R420.00, Ouma's Rusk Tin R195.00, Serving Bowl R640.00, Stoneware Mug R285.00 and Table Runner R560.00.
- Saltpan Ceramics is shown for the mug and the bowl, Dwarsrivier Linen for the apron and the runner, and nothing for the rusk tin.
- Each product page shows its name, price and description, and "Made by" only when there is a maker.
- After the mug twice and the apron once, the cart shows Stoneware Mug, 2, R570.00 and Linen Apron, 1, R420.00, total R990.00, and the header says "Cart (3)".
- After removing the apron, the cart shows only the mug, 2, R570.00, total R570.00, and "Cart (2)".
/?r=product&id=999and/?r=nothingshow the shop's "Not found" page.- The terminal running the server shows one line per request with its status, and no errors.
If it did not work, the error names the file and line. The commonest slips are a missing require line in index.php (Call to undefined function), a missing ?> or <?php in a page template, and a file saved in the wrong folder, such as src/cart.php in place of src/pages/cart.php (Failed opening required).
Task 3: Tour the shop and record a baseline
| # | Method and address | Status | Response headers that matter |
|---|---|---|---|
| 1 | GET / | 200 | Content-Type: text/html; charset=utf-8, Set-Cookie: tbo_session=...; path=/; HttpOnly; SameSite=Lax, X-Powered-By: PHP/8.5.12, Expires: Thu, 19 Nov 1981 08:52:00 GMT, Cache-Control: no-store, no-cache, must-revalidate, Pragma: no-cache |
| 2 | GET /style.css | 200 | Content-Type: text/css; charset=UTF-8. No X-Powered-By: the server sent the file without running PHP. |
| 3 | GET /?r=product&id=5 | 200 | Content-Type, X-Powered-By and the three cache headers. No Set-Cookie: the browser already has the cookie. |
| 4 | POST /?r=cart | 303 | Location: /?r=cart, X-Powered-By, the cache headers. |
| 5 | GET /?r=cart | 200 | As request 3. |
| 6 | GET /?r=product&id=999 | 404 | As request 3. The shop's "Not found" page. |
| 7 | GET /?r=nothing | 404 | As request 3. The shop's "Not found" page. |
A request for /favicon.ico with status 404 may also appear: the browser asks for an icon, and the shop has none.
The session cookie: name tbo_session, domain localhost, path /, expires at the end of the session, HttpOnly yes, Secure no (plain HTTP on my own computer), SameSite Lax.
Not sent yet, on any response: Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, Referrer-Policy and Strict-Transport-Security. Module 4 adds the first four on every response, and the last only over HTTPS, so it stays absent locally. X-Powered-By names the PHP version: Module 6 turns it off on the live server.
- Product list: shows the five products with maker and price, each linking to its page.
- Product page: name, maker when known, price, description, and an "Add to cart" form sent by POST with a hidden token.
- Cart: each line with quantity, amount and a Remove form, and the total, with prices taken from the database.
- Refusals: an unknown product or route gets the shop's own 404 page.
- Not built yet: accounts, sign-in and a customer's orders (Module 2), search, a quantity field and product pictures (Module 3), the security headers (Module 4), checkout and payment (Module 5), and Jason's and Naledi's staff pages for products, orders and the log (Modules 2, 3, 5 and 7).
Check your table against three points. The POST is 303 with Location, because the shop redirects after every form. Only the first response sets the cookie, because the session is created once and the browser sends the cookie back after that. And the two refusals are 404 pages drawn by the shop, not the server's grey page. If your first row has no Set-Cookie, you already had a session from an earlier visit: close the browser and repeat the tour, or delete the cookie in the Storage or Application tab first.
Task 4: Run the first checks and add a positive check of your own
The first run gives three PASS lines and All checks passed., as section 7 shows. With the shop stopped, the script prints Is the shop running at http://localhost:8000? and exits with code 2, so it can never report a pass for a shop that is not there.
Two good sentences: "The page shows Ouma's Rusk Tin, but the source sends <h1>Ouma's Rusk Tin</h1>, with the apostrophe as a character reference. That shows the name passed through e() on its way out, so the browser treats it as text and it can never become part of the page's markup."
The new check, added above the marker line:
$tin = $browser->get('/?r=product&id=5');
$failed += check('a name with an apostrophe is shown as text, not markup',
str_contains($tin['body'], '<h1>Ouma's Rusk Tin</h1>'));
The last run:
PASS the catalogue returns 200 and lists the sample products
PASS an unknown product returns 404
PASS an item added to the cart is still there on the next request
PASS a name with an apostrophe is shown as text, not markup
All checks passed.
Why it works: the check looks for the encoded form inside the heading, which only appears if the value was encoded. Product 5 is the rusk tin because the setup script inserts the samples in a fixed order. Other good versions look for Ouma's anywhere on the page, or use htmlspecialchars("Ouma's Rusk Tin", ENT_QUOTES) to build the expected text, as the first check does. The name describes the control working. A weaker version only checks that the page returns 200, which would still pass if the name were printed without e().