Kritim Yantra
Mar 25, 2025
Authentication is a crucial part of web applications, ensuring that users can securely log in, register, and manage their accounts. In this guide, we'll build a secure PHP authentication system using MySQL from scratch. This system will include:
Before starting, ensure you have:
First, let's create a MySQL database and a users
table.
CREATE DATABASE php_auth;
USE php_auth;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
id
→ Auto-incremented primary key.username
& email
→ Unique to prevent duplicates.password
→ Will store hashed passwords (never store plain text!).created_at
→ Tracks registration time.Organize your project like this:
php-auth/
│── includes/
│ ├── config.php # Database connection
│ ├── functions.php # Helper functions
│ └── auth.php # Auth logic (login, register)
│── index.php # Homepage
│── register.php # Registration form
│── login.php # Login form
│── dashboard.php # Protected page
│── logout.php # Logout script
config.php
)<?php
$host = 'localhost';
$dbname = 'php_auth';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
register.php
)<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form action="includes/auth.php" method="POST">
<input type="text" name="username" placeholder="Username" required><br>
<input type="email" name="email" placeholder="Email" required><br>
<input type="password" name="password" placeholder="Password" required><br>
<button type="submit" name="register">Register</button>
</form>
<p>Already have an account? <a href="login.php">Login here</a></p>
</body>
</html>
auth.php
)<?php
session_start();
require 'config.php';
if (isset($_POST['register'])) {
$username = $_POST['username'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
try {
$stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$stmt->execute([$username, $email, $password]);
$_SESSION['success'] = "Registration successful! You can now login.";
header("Location: login.php");
exit();
} catch (PDOException $e) {
$_SESSION['error'] = "Registration failed: " . $e->getMessage();
header("Location: register.php");
exit();
}
}
?>
password_hash()
→ Securely hashes passwords.PDO Prepared Statements
→ Prevents SQL injection.$_SESSION
→ Stores success/error messages.login.php
)<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<?php if (isset($_SESSION['error'])): ?>
<p style="color: red;"><?= $_SESSION['error']; ?></p>
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
<form action="includes/auth.php" method="POST">
<input type="text" name="username" placeholder="Username" required><br>
<input type="password" name="password" placeholder="Password" required><br>
<button type="submit" name="login">Login</button>
</form>
<p>Don't have an account? <a href="register.php">Register here</a></p>
</body>
</html>
auth.php - Continued
)if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: dashboard.php");
exit();
} else {
$_SESSION['error'] = "Invalid username or password.";
header("Location: login.php");
exit();
}
}
password_verify()
→ Checks hashed passwords.$_SESSION['user_id']
→ Stores logged-in user data.logout.php
)<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit();
?>
password_hash()
and password_verify()
.You've now built a secure PHP authentication system with MySQL! This includes:
🚀 Happy Coding! 🚀
Let me know in the comments if you have any questions! 😊👇
No comments yet. Be the first to comment!
Please log in to post a comment:
Continue with Google