PHP Lab 1: User Registration & Login

Lab Objectives

Prerequisites

Step 1: Database Setup

Create database and table in MySQL:


CREATE DATABASE php_lab;
USE php_lab;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
    

Step 2: PHP File Structure

Step 3: PHP Scripts

See lab manual or starter code for:

Step 4: Exercises

  1. Set up XAMPP / LAMP on your machine
  2. Create the database and table using MySQL
  3. Create PHP files according to the folder structure
  4. Test registration → login → dashboard → logout
  5. Try registering a duplicate username and observe the error
  6. Try logging in with incorrect credentials and observe the output
  7. Check database to see that passwords are hashed
  8. Bonus: add a "last login" timestamp

Questions

  1. Explain the difference between Input, Processing, Output, and Storage in this lab.
  2. Why do we use password_hash() instead of storing passwords directly?
  3. How does a prepared statement help prevent SQL injection?
  4. What is the purpose of session_start() in the login system?
  5. What happens if a user tries to login with a wrong password multiple times? How could you improve security?
  6. Explain what would happen if we didn’t validate input in registration.

Expected Learning Outcomes

Note: Save all your files under the folder first_php_lab and run them locally. Do not upload to a public server as passwords are sensitive data.