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
-
1Two pages, one headercourses.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.
-
2The application formapply.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.
-
3Attack one: markupSubmit <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.
-
4Save itCreate 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.
-
5Attack two: SQLSubmit 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.
-
6Gate itlogin.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
| Criterion | Points |
|---|---|
| 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 |
صورتحال
ایک coaching centre ایک چھوٹی site چاہتا ہے: ایک صفحے پر courses، ایک application form جو database میں save ہو، اور ایک نجی صفحہ جہاں staff enquiries پڑھے۔ آپ اسے PHP میں اس طرح بنائیں گے جیسے کام کرنے والا developer بناتا ہے — مشترکہ حصوں کے لیے include، server-side validation، prepared statements — اور پھر اپنے ہی form پر دو بار حملہ کر کے ثابت کریں گے کہ یہ ٹکتا ہے۔
آپ کیا دکھا سکیں گے
- PHP سے صفحے serve کرنا جہاں code کبھی browser تک نہ پہنچے
- server پر validate کرنا اور form کو شائستگی سے دوبارہ دکھانا
- prepared statements سے save اور list کرنا، اور ثابت کرنا کہ SQL injection ناکام ہوتی ہے
- ایک صفحے کو session اور hashed password کے پیچھے رکھنا
آپ کو کیا چاہیے
- آپ کی machine پر XAMPP (Apache، PHP، MySQL)
- ایک browser اور اس کا view-source
- سبق 5 اور 6 کا ابتدائی code
کام
-
1دو صفحے، ایک headercourses.php پانچ courses کی array کو styled table میں loop کرے؛ about.php میں centre کی تفصیلات ہوں۔ دونوں nav والے header.php اور footer.php کو include کریں۔ nav کا متن ایک بار بدلیں اور تصدیق کریں کہ دونوں صفحے بدلے۔ browser کی طرف view source کریں: کوئی PHP نظر نہ آئے۔درست نتیجہ: ایک header بانٹتے دو صفحے؛ source view جس میں صرف HTML ہو۔
-
2application formapply.php اپنا POST خود سنبھالے: نام، email، course کا انتخاب، message۔ server پر $errors array میں validate کریں، ناکامی پر پرانی values کے ساتھ دوبارہ دکھائیں، کامیابی پر شکریہ کہیں۔ visitor کے data کی ہر echo htmlspecialchars() سے گزرے۔درست نتیجہ: خالی submission اچھے fields رکھتے ہوئے errors دکھائے؛ درست submission شکریہ دکھائے۔
-
3حملہ ایک: markupmessage میں <b>bold</b> اور ایک <script>alert(1)</script> tag جمع کروائیں۔ تصدیق کریں کہ دونوں جہاں بھی دکھائے جائیں بے ضرر متن کے طور پر نظر آئیں۔درست نتیجہ: tags لفظی متن کے طور پر نظر آئیں؛ نہ bold، نہ alert۔
-
4save کریںdatabase اور enquiries table بنائیں۔ db.php میں PDO connection ہو۔ apply.php درست submissions کو prepared statement سے INSERT کرے۔ enquiries.php تمام قطاریں نئی-پہلے دکھائے، ہر cell پر htmlspecialchars کے ساتھ۔درست نتیجہ: ایک submission enquiries.php میں سب سے اوپر نظر آئے۔
-
5حملہ دو: SQLایسا message جمع کروائیں جس میں '; DROP TABLE enquiries; -- ہو اور پھر enquiries.php reload کریں۔درست نتیجہ: table اب بھی موجود ہو اور message اس میں بے ضرر متن کے طور پر نظر آئے۔
-
6دروازہ لگائیںlogin.php ایک admin user کو ایک بار بنائے گئے password_hash کے مقابل password_verify سے چیک کرے۔ sessions enquiries.php کی حفاظت کریں؛ logout link شامل کریں۔ سبق کے folder ڈھانچے میں ترتیب دیں۔ پورا flow ایک حقیقی شخص کو شروع سے آخر تک demo کریں، ہر حصہ بلند آواز میں سمجھاتے ہوئے۔درست نتیجہ: sign out ہونے پر enquiries.php login پر بھیجے؛ demo دیا گیا اور وہ شخص بتا سکے کہ ہر صفحہ کیا کرتا ہے۔
کیا جمع کروانا ہے
project folder (zip)، دونوں حملوں کے ناکام ہونے کے screenshots، اور جس شخص کو demo دیا اس کا نام اور ایک line کہ وہ کیا سمجھا۔
نمبر کیسے ملیں گے
| معیار | نمبر |
|---|---|
| مشترکہ header کام کرتا ہے اور کوئی PHP browser تک نہیں پہنچتی | 15 |
| form server پر validate کرتا ہے اور شائستگی سے دوبارہ دکھاتا ہے | 20 |
| markup حملہ ہر جگہ متن کے طور پر دکھتا ہے | 15 |
| prepared statements درست save اور list کرتے ہیں | 15 |
| SQL حملہ ناکام ہوتا ہے اور table بچ جاتی ہے | 15 |
| hashed password والا login صفحے کی حفاظت کرتا ہے؛ demo دیا گیا | 20 |
| کل · پاس 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 IDAlready have one? Sign in and this course will be added to it.