PHP Lab 1: User Registration & Login
Lab Objectives
- Understand web app workflow: Input → Processing → Output → Storage
- Create PHP scripts to handle forms and user input
- Connect PHP to MySQL database
- Implement basic security practices: password hashing and input validation
- Manage user sessions for login and logout
Prerequisites
- XAMPP / WAMP / LAMP installed
- Basic HTML & PHP knowledge
- Browser (Chrome / Firefox)
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
- config.php (Database connection)
- register.php (User registration)
- login.php (Login page)
- dashboard.php (Welcome dashboard)
- logout.php (Logout)
- style.css (Optional styling)
Step 3: PHP Scripts
See lab manual or starter code for:
- config.php
- register.php
- login.php
- dashboard.php
- logout.php
Step 4: Exercises
- Set up XAMPP / LAMP on your machine
- Create the database and table using MySQL
- Create PHP files according to the folder structure
- Test registration → login → dashboard → logout
- Try registering a duplicate username and observe the error
- Try logging in with incorrect credentials and observe the output
- Check database to see that passwords are hashed
- Bonus: add a "last login" timestamp
Questions
- Explain the difference between Input, Processing, Output, and Storage in this lab.
- Why do we use
password_hash() instead of storing passwords directly?
- How does a prepared statement help prevent SQL injection?
- What is the purpose of
session_start() in the login system?
- What happens if a user tries to login with a wrong password multiple times? How could you improve security?
- Explain what would happen if we didn’t validate input in registration.
Expected Learning Outcomes
- Understand how web forms interact with PHP and databases
- Implement secure user authentication
- Understand session management for logged-in users
- Gain confidence in basic PHP web application development
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.