Creating a User Login System with PHP and MySQL

Creating a User Login System with PHP and MySQL

PHP Login & Registration System using MySQL & Bootstrap

This guide will walk you through building a secure login and registration system using PHP, MySQL, and Bootstrap.

Step 1: Create MySQL Table

CREATE TABLE users ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, email VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );

Run the SQL query in your MySQL database to create the users table.

Step 2: config.php – Database Connection

<?php define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', '123456'); define('DB_NAME', 'login_system'); $conection_db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); if ($conection_db === false) { die("ERROR: Could not connect. " . mysqli_connect_error()); } ?>

Step 3: php_register.php – Registration Logic

<?php $email = $password = $confirm_password = ""; $email_err = $password_err = $confirm_password_err = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { require_once "config.php"; // Email validation if (empty(trim($_POST["email"]))) { $email_err = "Please enter an email."; } else { $sql = "SELECT id FROM users WHERE email = ?"; if ($stmt = mysqli_prepare($conection_db, $sql)) { mysqli_stmt_bind_param($stmt, "s", $param_email); $param_email = trim($_POST["email"]); if (mysqli_stmt_execute($stmt)) { mysqli_stmt_store_result($stmt); if (mysqli_stmt_num_rows($stmt) == 1) { $email_err = "This email is already taken."; } else { $email = trim($_POST["email"]); } } else { echo "Oops! Something went wrong."; } mysqli_stmt_close($stmt); } } // Password validation if (empty(trim($_POST["password"]))) { $password_err = "Please enter a password."; } elseif (strlen(trim($_POST["password"])) < 6) { $password_err = "Password must have at least 6 characters."; } else { $password = trim($_POST["password"]); } // Confirm password if (empty(trim($_POST["confirm_password"]))) { $confirm_password_err = "Please confirm password."; } else { $confirm_password = trim($_POST["confirm_password"]); if (empty($password_err) && ($password != $confirm_password)) { $confirm_password_err = "Passwords do not match."; } } // Insert if no errors if (empty($email_err) && empty($password_err) && empty($confirm_password_err)) { $sql = "INSERT INTO users (email, password) VALUES (?, ?)"; if ($stmt = mysqli_prepare($conection_db, $sql)) { mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_password); $param_email = $email; $param_password = password_hash($password, PASSWORD_DEFAULT); if (mysqli_stmt_execute($stmt)) { header("location: login.php"); } else { echo "Something went wrong."; } mysqli_stmt_close($stmt); } } mysqli_close($conection_db); } ?>

Step 4: register.php – Registration Form

<?php require_once 'config.php'; include 'php_register.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <title>Register</title> <link rel="stylesheet" href="assets/css/style.css"> </head> <body> <div class="form-container"> <form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <h2>Register</h2> <input type="email" name="email" value="<?= $email; ?>" placeholder="Email"> <span class="error"><?= $email_err; ?></span> <input type="password" name="password" placeholder="Password" value="<?= $password; ?>"> <span class="error"><?= $password_err; ?></span> <input type="password" name="confirm_password" placeholder="Confirm Password" value="<?= $confirm_password; ?>"> <span class="error"><?= $confirm_password_err; ?></span> <button type="submit">Register</button> <p>Already have an account? <a href="login.php">Login</a></p> </form> </div> </body> </html>

Step 5: php_login.php – Login Logic

<?php $email = $password = ""; $email_err = $password_err = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { require_once "config.php"; if (empty(trim($_POST["email"]))) { $email_err = "Please enter email."; } else { $email = trim($_POST["email"]); } if (empty(trim($_POST["password"]))) { $password_err = "Please enter your password."; } else { $password = trim($_POST["password"]); } if (empty($email_err) && empty($password_err)) { $sql = "SELECT id, email, password FROM users WHERE email = ?"; if ($stmt = mysqli_prepare($conection_db, $sql)) { mysqli_stmt_bind_param($stmt, "s", $param_email); $param_email = $email; if (mysqli_stmt_execute($stmt)) { mysqli_stmt_store_result($stmt); if (mysqli_stmt_num_rows($stmt) == 1) { mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password); if (mysqli_stmt_fetch($stmt)) { if (password_verify($password, $hashed_password)) { session_start(); $_SESSION["loggedin"] = true; $_SESSION["id"] = $id; $_SESSION["email"] = $email; header("location: welcome.php"); } else { $password_err = "Invalid password."; } } } else { $email_err = "No account found with that email."; } } else { echo "Oops! Something went wrong."; } mysqli_stmt_close($stmt); } } mysqli_close($conection_db); } ?>

Step 6: login.php – Login Form

<?php session_start(); if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) { header("location: welcome.php"); exit; } require_once "config.php"; require_once "php_login.php"; ?> <!DOCTYPE html> <html lang="en"> <head> <title>Login</title> <link rel="stylesheet" href="assets/css/style.css"> </head> <body> <div class="form-container"> <form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <h2>Login</h2> <input type="email" name="email" value="<?= $email; ?>" placeholder="Email"> <span class="error"><?= $email_err; ?></span> <input type="password" name="password" placeholder="Password"> <span class="error"><?= $password_err; ?></span> <button type="submit">Login</button> <p>Don't have an account? <a href="register.php">Sign up</a></p> </form> </div> </body> </html>

Step 7: welcome.php – Protected Page

<?php session_start(); if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) { header("location: login.php"); exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Welcome</title> </head> <body> <h1>Welcome, <?= htmlspecialchars($_SESSION["email"]); ?>!</h1> <p><a href="logout.php">Logout</a></p> </body> </html>

Step 8: logout.php – Destroy Session

<?php session_start(); $_SESSION = array(); session_destroy(); header("location: login.php"); exit; ?>

Let me know if you want me to zip this into a complete working starter project or add extra features like:

  • Password reset

  • Email verification

  • Admin panel



Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close