A lightweight PHP/MySQL admin authentication gateway with secure sessions, CSRF protection, OTP verification, login throttling, and audit logs.
SecureAuth Admin is a small, serious authentication layer for PHP projects. It keeps the surface area easy to review while covering the parts that matter most: password hashing, strict cookies, session expiry, CSRF tokens, optional email OTP verification, IP lockouts, safe logging, and a first-admin setup flow.
It is not trying to be a giant framework. It is the front door.
- Drop-in PHP admin login system for dashboards, panels, internal tools, and small SaaS back offices.
- Secure login starter with PDO, MySQL, CSRF defense, OTP email verification, session hardening, and audit logs already wired together.
- Beginner-friendly XAMPP setup, but written with production habits like
.envsecrets, Apache deny rules, and safe logging. - MIT licensed, so you can use it in personal, commercial, client, and learning projects.
php authentication, php login system, secure admin login, php mysql login, admin dashboard login, csrf protection, two factor authentication, email otp, pdo mysql, xampp php project, php security, audit logs, login throttling, secure session management
| Document | What it covers |
|---|---|
| Project documentation | Step-by-step setup, configuration, login flow, operations, and troubleshooting. |
| Security review | Code-level security coverage, remaining responsibilities, and manual test checklist. |
| Contributing guide | How to report bugs, request features, and send clean pull requests. |
| Changelog | Version history for releases. |
| GitHub SEO checklist | Repository topics, description, social preview, and engagement settings. |
| Database schema | Tables for admins, login attempts, and audit logs. |
| .env example | Safe template for local and server configuration. |
| Area | Built-in behavior |
|---|---|
| Passwords | Uses PHP password_hash() and password_verify() with automatic rehash support. |
| Sessions | Uses strict cookies, HttpOnly, SameSite=Strict, session ID regeneration, idle timeout, absolute timeout, and user-agent fingerprinting. |
| CSRF | Protects every POST flow with random tokens and hash_equals(). |
| Login abuse | Tracks failed attempts by IP and applies a lockout window. |
| 2FA | Supports optional email OTP with hashed codes, expiry, and attempt limits. |
| Audit trail | Records login, logout, OTP, setup, dashboard, lockout, and CSRF events. |
| Headers | Sends CSP, frame protection, MIME sniffing protection, referrer policy, permissions policy, and no-store cache headers. |
| Setup | Creates the first admin only while the admin table is empty. |
| Path | Purpose |
|---|---|
config.php |
Environment loading, PDO setup, secure sessions, headers, CSRF helpers, auth guard, logging, and shared helpers. |
index.php |
Login screen, credential verification, lockout handling, and OTP challenge. |
dashboard.php |
Protected dashboard with auth health stats and recent audit activity. |
logout.php |
CSRF-protected logout with full session destruction. |
setup.php |
First-admin setup screen. Locked after the first admin exists. |
mail.php |
PHPMailer SMTP delivery for admin OTP email. |
assets/ |
CSS, JavaScript, and the project logo. |
database/schema.sql |
The schema this app expects. |
storage/ |
Runtime logs and sessions. Kept out of Git except .gitkeep files. |
-
Put the folder inside your web server document root.
For XAMPP on Windows, this project is commonly served from:
C:\xampp\htdocs\secure-login -
Copy the environment template.
Copy-Item .env.example .env -
Open
.envand fill the real values.Set
APP_URL, create a strongAPP_KEY, confirm the database values, and adjustSESSION_COOKIE_PATHif the folder name changes. -
Create the database and import the schema.
CREATE DATABASE `secure-login` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'secure_login_user'@'localhost' IDENTIFIED BY 'use-a-strong-password'; GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, ALTER ON `secure-login`.* TO 'secure_login_user'@'localhost'; FLUSH PRIVILEGES;
cmd /c "mysql --default-character-set=utf8mb4 -u secure_login_user -p secure-login < database\schema.sql"
-
Open
setup.phpin the browser and create the first admin.Local requests can run setup without a setup token. On a public server, set
SETUP_TOKENin.envfirst and opensetup.php?token=your-token. -
Log in at
index.php.After the first admin exists, setup closes itself automatically.
| Key | Why it matters |
|---|---|
APP_KEY |
Must be at least 32 random characters. Used for keyed hashes and session fingerprinting. |
APP_URL |
Should match the exact URL where the app is served. |
APP_FORCE_HTTPS |
Use true only when the app is really served through HTTPS. |
DB_* |
Use a dedicated database user instead of a root account. |
SESSION_COOKIE_PATH |
Set this to the deployed folder path, such as /secure-login. |
SETUP_TOKEN |
Required for safer first-admin setup on public servers. |
MAIL_* |
Required only when an admin has email OTP enabled. |
The important security behavior is documented in docs/SECURITY.md. In short, the app is designed to fail closed, avoid leaking secrets, and make authentication events visible through audit logs.
Before sharing the repository, keep these files out of commits:
.env
storage/logs/*
storage/sessions/*
backups
real database dumps
temporary files
The included .gitignore already handles the common cases. Still, review git status --short before every commit.
Useful improvements are welcome:
- Stronger first-admin password rules.
- Account-level throttling in addition to IP throttling.
- Admin management screen for enabling OTP without SQL.
- Automated tests for login, CSRF, lockout, OTP expiry, and setup lockout.
- Translations for setup and login screens.
php -l config.php
php -l index.php
php -l dashboard.php
php -l logout.php
php -l setup.php
php -l mail.phpThen test these flows in the browser:
- Wrong password shows a generic error and increments the lockout counter.
- Correct password creates a new authenticated session.
- Dashboard redirects to login when no session exists.
- Logout works only through POST with a valid CSRF token.
- OTP login works when SMTP is configured and
twofa_enabled = 1.
Released under the MIT License. You can use, copy, modify, publish, distribute, sublicense, and sell copies of this project as long as the license notice stays included.
