A secure, modular User Authentication System built in PHP. Ideal for learning, extending, or integrating basic auth flows into your applications.
- Overview
- Features
- Tech Stack
- Architecture & Folder Structure
- Installation & Setup
- Usage
- Security Notes
- Contributing
- License
This project demonstrates a clean PHP‑based authentication flow with session management. Great for anyone learning PHP OOP or building foundational auth features before adopting full-stack frameworks. It's a work‑in‑progress that’s evolving with deeper OOP practices, PSR‑compliance, and enhanced security practices.
- User Registration, Login, and Logout
- Persistent session management (via
$_SESSION
) - Password hashing using PHP’s
password_hash()
andpassword_verify()
- Input validation & graceful error / field validation handling
- Structured for OOP refactoring, extensibility & reuse
- PHP (native, no framework)
- MySQL (or compatible RDBMS via PDO)
- Composer-ready structure (autoload ready)
- Optional front-end enhancements (e.g., Bootstrap, client-side validation)
/ (root)
├── src/ # Core PHP classes (e.g. User, Auth, DB)
├── public/ # Entry points: index.php, login.php, register.php, logout.php
├── config/ # database.php, config.php for environment vars
├── templates/ # Shared header/footer or email templates
├── assets/ # JS / CSS / images
├── vendor/ # Composer dependencies (optional)
└── README.md
- Classes live under
src/
, following PSR‑4 standards. - Public MVC-like controllers (
public/*.php
) handle form data, instantiate Auth logic, and render templates. config/
centralizes DB and session settings.
- PHP 7.4+ with PDO (MySQL or MariaDB)
- MySQL 5.7+
- Composer (optional, if using autoloading)
-
Clone the repo
git clone https://github.com/MisaghMomeniB/User-Auth-System-PHP.git
-
Import DB schema (
schema.sql
) into your database. -
Configure DB connection (
config/config.php
):define('DB_HOST', 'localhost'); define('DB_NAME', 'your_db'); define('DB_USER', 'db_user'); define('DB_PASS', 'secret');
-
(Optional) Install dependencies & autoload:
composer install
-
Serve the project:
-
For built-in PHP server:
php -S localhost:8000 -t public/
-
Or configure with Apache/Nginx.
-
- Register: Open
/register.php
, fill the form to create an account - Login: Via
/login.php
, then access protected areas - Logout: Via
/logout.php
, cleans session & redirects - Session Check: Ensure protection using middleware pattern in your secured pages
- Uses
password_hash()
/password_verify()
for password safety - Uses prepared statements via PDO to prevent SQL injection
- Session‑based authentication with regeneration of session IDs after login
- Next steps: CSRF tokens, login throttling, email verification, 2FA — upcoming in this repo!
Contributions welcome!
- Fork the repo
- Create a feature branch (
feature/…
) - Commit your changes with clear messages
- Open a Pull Request and describe your changes
This project is licensed under the MIT License. Feel free to use, modify, and distribute it.
Let me know if you'd like:
- Example
.env
/ config templates - Basic CSRF / 2FA implementation
- Commented code blocks or diagrams