Secure Login System

This Secure Login System example is a practical and reliable web-based application built with PHP and MySQL that demonstrates how real-world websites manage user authentication securely. Instead of storing plain-text passwords, this system uses password hashing (encryption) to protect user credentials. Users can register with a unique username and password, log in securely using hashed credentials, access a protected dashboard, and log out safely

Folder Structure

	login-system/
		│
		├── db.php               ← Database connection
		├── register.php         ← Registration form & logic
		├── login.php            ← Login form & logic
		├── dashboard.php        ← Protected page
		├── logout.php           ← Logout logic
		├── style.css            ← Styling
	

Step-by-Step Files

db.php

		 
		<?php
			$conn = new mysqli("localhost", "root", "", "login_demo");
			if ($conn->connect_error) {
			    die("Connection failed: " . $conn->connect_error);
			}
		?>
		 
	

🔍 What's happening here?

You're creating a new connection to your MySQL database using PHP.


register.php

		 
		<?php
		require 'db.php';
		
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
		    $username = $_POST["username"];
		    $password = password_hash($_POST["password"], PASSWORD_DEFAULT);
		
		    $stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
		    $stmt->bind_param("ss", $username, $password);
			 
		    try{
	                $stmt->execute();
	                echo "Registered successfully!";
	            } catch (mysqli_sql_exception $e) {
	                echo "Username already taken.";
	            }
		}
		?>

		<link rel="stylesheet" href="style.css">
		<form method="post">
		    <h2>Register</h2>
		    Username: <input name="username" required><br>
		    Password: <input type="password" name="password" required><br>
		    <button type="submit">Register</button>
		</form>
		 
	

🔍 What’s happening in the registration logic?

This PHP code runs when a user submits the registration form. Let’s break it down step by step:


login.php

		 
		<?php
		session_start();
		require 'db.php';
		
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
		    $username = $_POST["username"];
		    $password = $_POST["password"];
		
		    $stmt = $conn->prepare("SELECT id, password FROM users WHERE username = ?");
		    $stmt->bind_param("s", $username);
		    $stmt->execute();
		    $stmt->store_result();
		
		    if ($stmt->num_rows === 1) {
		        $stmt->bind_result($id, $hashed_password);
		        $stmt->fetch();
		        if (password_verify($password, $hashed_password)) {
		            $_SESSION["user_id"] = $id;
		            header("Location: dashboard.php");
		            exit;
		        }
		    }
		    echo "Invalid username or password.";
		}
		?>

		<link rel="stylesheet" href="style.css">
		<form method="post">
		    <h2>Login</h2>
		    Username: <input name="username" required><br>
		    Password: <input type="password" name="password" required><br>
		    <button type="submit">Login</button>
		</form>
		 
	

🔍 What’s happening in the login logic?

This PHP code runs when a user submits the login form. Let’s break it down step by step:


dashboard.php

		 
		<link rel="stylesheet" href="style.css">
		<?php  
		    session_start();  
		    if (!isset($_SESSION["user_id"])) {  
		        header("Location: login.php");  
		        exit;  
		    }  
		    echo "<h2>Welcome! You are logged in.</h2>";  
		    echo "<a href='logout.php'>Logout</a>";  
		?>
		 
	

🔍 What’s happening in the dashboard logic?

This PHP code makes sure only users who are logged in can access the dashboard. Let’s break it down step by step:

✅ This protects your page from being accessed directly by someone who isn’t logged in. Without this check, anyone could visit dashboard.php even without logging in!


logout.php

		 
		<?php
		session_start();
		session_destroy();
		header("Location: login.php");
		exit;
		?>
		 
	

🔍 What's happening here?

This PHP script logs the user out by ending the session and redirecting them back to the login page.


style.css

		 
		body {
		    font-family: Arial, sans-serif;
		    background-color: #f7f7f7;
		    padding: 30px;
		}
		form {
		    background: #fff;
		    padding: 20px;
		    border-radius: 8px;
		    box-shadow: 0 0 10px #ccc;
		}
		input {
		    margin-bottom: 10px;
		    padding: 8px;
		    width: 200px;
		}
		button {
		    padding: 10px;
		}
		 
	

Setup Instructions

		1. Create a database: login_demo
		2. Run this SQL:
		   CREATE TABLE users (
		       id INT AUTO_INCREMENT PRIMARY KEY,
		       username VARCHAR(50) NOT NULL UNIQUE,
		       password VARCHAR(255) NOT NULL
		   );
		3. Open register.php to create a user
		4. Open login.php to log in
		5. dashboard.php is protected