Project Objective
n this mini project, we will build a secure Login and Registration system using Object-Oriented PHP, MySQL, and sessions.
The goal of this project is not just to create a login form, but to understand how real PHP applications organize authentication logic using OOP principles.
By completing this project, students will learn how to:
- Structure a PHP project using classes
- Separate database logic from application logic
- Securely store user passwords
- Manage user sessions
- Build a basic authentication system
This project will help you apply several important OOP concepts in a practical way.
OOP Concepts Used
This project reinforces several Object-Oriented Programming concepts that you have learned earlier.
Some important concepts used in this project include:
- Classes and Objects
- Constructors
- Encapsulation
- Database abstraction
- Session management
- Password hashing
- Separation of concerns
Understanding how these concepts work together is essential when building real PHP applications.
Suggested Folder Structure
A clean folder structure helps keep the project organized and easier to maintain.
For this project, the following structure is recommended:
oop-login/
│
├── config/
│ └── Database.php
│
├── classes/
│ └── User.php
│
├── public/
│ ├── register.php
│ ├── login.php
│ ├── dashboard.php
│ └── logout.php
│
└── index.php
Explanation of folders:
- config/ → Contains database configuration files
- classes/ → Contains application classes
- public/ → Contains user-facing pages
- index.php → Entry point of the application
Organizing files this way keeps your application modular and scalable.
🔹 Step 1: Database Class (Core Responsibility)
The first step is to create a Database class that will manage the connection to MySQL.
Instead of connecting to the database in multiple files, we create a single reusable class that handles the connection.
The responsibilities of the Database class are:
- Establish a connection with MySQL
- Store the connection privately
- Provide a method to access the connection
Example structure:
class Database {
private $conn;
public function connect() {
// return mysqli connection
}
}
This approach ensures that the database connection can be reused throughout the application.
🔹 Step 2: User Class (Business Logic)
Next, we create a User class that handles the core business logic of our authentication system.
This class will be responsible for:
- Registering new users
- Logging in existing users
- Validating user credentials
The User class should contain:
- A private database property
- A constructor that receives the database connection
- Methods for registration and login
Basic structure:
register()
login()
An important rule to remember:
The User class should only contain business logic.
It should not include any HTML code.
Keeping logic separate from presentation is a key principle in professional PHP development.
🔹 Step 3: Registration Logic
The registration process allows users to create a new account.
During registration, the application should:
- Receive form data
- Hash the user’s password
- Insert the data into the database
- Prevent duplicate email registrations
Password hashing is extremely important for security.
PHP provides a built-in function for this purpose:
password_hash($password, PASSWORD_DEFAULT);
This ensures that passwords are never stored in plain text.
🔹 Step 4: Login Logic
The login process verifies whether the user credentials are valid.
The login system should perform the following steps:
- Check if the email exists in the database
- Compare the entered password with the stored hashed password
- Start a user session after successful authentication
- Store the user ID in the session
To verify passwords securely, PHP provides the function:
password_verify($inputPassword, $dbPassword);
This function compares the entered password with the stored hash safely.
🔹 Step 5: Session Management
Sessions allow PHP to remember a logged-in user across multiple pages.
Important session rules for this project include:
- Start a session when the user logs in
- Store the user ID inside the session
- Protect restricted pages like dashboard.php
- Destroy the session when the user logs out
Common session functions used in this project:
session_start();
session_destroy();
Proper session management is essential for building secure authentication systems.
Using the Classes in Login & Registration Pages
After creating the Database and User classes, the next step is to use them inside your application pages such as register.php and login.php.
These pages act as the entry points of the application, where form data is collected and passed to the appropriate class methods.
For example, the registration page will:
- Include the required class files
- Create a database connection
- Create a
Userobject - Call the
register()method with the submitted form data
Similarly, the login page will:
- Include the required class files
- Create a database connection
- Create a
Userobject - Call the
login()method to verify the user’s credentials
This approach keeps the application organized because:
- Pages handle user interaction (forms and UI)
- Classes handle business logic and database operations
Example flow:
register.php
Include Database.php
Include User.php
Create database connection
Create User object
Call register() method
login.php
Include Database.php
Include User.php
Create database connection
Create User object
Call login() method
By separating responsibilities in this way, your application becomes easier to maintain, debug, and extend in the future.
🔐 Security Rules (Must Follow)
Security should always be a priority when building login systems.
Students should follow these important rules:
- Use prepared statements to prevent SQL injection
- Never store plain text passwords
- Always validate user inputs
- Use
htmlspecialchars()when displaying user data
Following these practices helps protect your application from common vulnerabilities.
🧪 Student Challenges (Very Important)
To improve your understanding, try completing the following challenges:
- Prevent duplicate email registration
- Add basic error handling for login failures
- Redirect logged-in users away from the login page
- Display a welcome message on the dashboard
These small tasks will help you think like a real PHP developer.
🚫 What NOT To Do
Avoid these common mistakes while building the project:
❌ Do not write procedural PHP code
❌ Do not run database queries directly inside page files
❌ Do not display or store user passwords
❌ Do not mix HTML and database logic in the same class
Keeping your code clean and modular is an important part of professional development.
✅ Expected Outcome
By completing this project, students will:
- Understand how authentication systems work
- Learn to structure PHP projects using OOP
- Build reusable and maintainable code
- Apply security best practices
Most importantly, this project will help you start thinking in Object-Oriented Programming instead of procedural PHP.
