PHP Basics · Course lab · about 240 minutes · 6 tasks · marked out of 100, pass at 60

An enquiry system that survives an attack on itself

The situation

A coaching centre wants a small site: courses on one page, an application form that saves to a database, and a private page where staff read the enquiries. You will build it in PHP the way a working developer does — include for shared parts, server-side validation, prepared statements — and then attack your own form twice to prove it holds.

What you'll be able to show

  • Serve pages from PHP where the code never reaches the browser
  • Validate on the server and redisplay a form courteously
  • Save and list with prepared statements, and prove SQL injection fails
  • Gate a page behind a session and a hashed password

What you need

  • XAMPP (Apache, PHP, MySQL) on your machine
  • A browser and its view-source
  • The lesson 5 and 6 starting code

Tasks

  1. 1Two pages, one header
    courses.php loops an array of five courses into a styled table; about.php has the centre's details. Both include header.php and footer.php with a nav. Change the nav text once and confirm both pages change. View source on the browser side: no PHP visible.
    A correct result: Two pages sharing one header; a source view containing only HTML.
  2. 2The application form
    apply.php handles its own POST: name, email, course choice, message. Validate on the server into an $errors array, redisplay with old values on failure, thank on success. Every echo of visitor data goes through htmlspecialchars().
    A correct result: A blank submission shows errors with the good fields kept; a valid one shows thanks.
  3. 3Attack one: markup
    Submit <b>bold</b> and a <script>alert(1)</script> tag in the message. Confirm both render as harmless text everywhere they are shown.
    A correct result: The tags appear as literal text; no bold, no alert.
  4. 4Save it
    Create the database and enquiries table. db.php holds the PDO connection. apply.php INSERTs valid submissions with a prepared statement. enquiries.php lists all rows newest first with htmlspecialchars on every cell.
    A correct result: A submission appears at the top of enquiries.php.
  5. 5Attack two: SQL
    Submit a message containing '; DROP TABLE enquiries; -- and then reload enquiries.php.
    A correct result: The table still exists and the message appears as harmless text in it.
  6. 6Gate it
    login.php checks one admin user with password_verify against a password_hash you generated once. Sessions gate enquiries.php; add a logout link. Organise into the folder structure from the lesson. Demo the whole flow to one real person, start to finish, explaining each part aloud.
    A correct result: enquiries.php redirects to login when signed out; the demo was given and the person could say what each page does.

What to hand in

The project folder (zipped), screenshots of both attacks failing, and the name of the person you demoed to with one line of what they understood.

How it is marked

CriterionPoints
Shared header works and no PHP reaches the browser 15
Form validates on the server and redisplays courteously 20
Markup attack renders as text everywhere 15
Prepared statements save and list correctly 15
SQL attack fails and the table survives 15
Login with hashed password gates the page; demo given 20
Total · pass at 60 100

Hand in your lab

Create a free BvLogic ID to hand in your lab, get it marked, and have it on your certificate.

Create your BvLogic ID

Already have one? Sign in and this course will be added to it.