Build Login and Registration System in PHP & MySQL (Complete Guide with Source Code)

 Build Login and Registration System in PHP & MySQL (Complete Guide with Source Code)

In this tutorial, we will build a complete Login and Registration System using PHP and MySQL. This is one of the most important real-world projects for every PHP developer.

By the end of this tutorial, you will have:

  • User Registration System

  • Secure Login System

  • Password Hashing

  • Session Management

  • Logout Feature


1. Project Structure

project/
│
├── db.php
├── register.php
├── login.php
├── dashboard.php
├── logout.php

2. Database Setup

Create a database:

CREATE DATABASE auth_system;

Create users table:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE,
    password VARCHAR(255)
);

3. Database Connection (db.php)

<?php

$conn = new mysqli("localhost","root","","auth_system");

if($conn->connect_error){
    die("Connection Failed");
}

?>

4. User Registration (register.php)

<?php
include "db.php";

if(isset($_POST['register'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    $sql = "INSERT INTO users(name,email,password)
            VALUES('$name','$email','$password')";

    if($conn->query($sql)){
        echo "Registration Successful";
    } else {
        echo "Error";
    }
}
?>

<form method="POST">
    <input type="text" name="name" placeholder="Name" 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>

5. User Login (login.php)

<?php
session_start();
include "db.php";

if(isset($_POST['login'])){

    $email = $_POST['email'];
    $password = $_POST['password'];

    $result = $conn->query("SELECT * FROM users WHERE email='$email'");

    if($result->num_rows > 0){

        $user = $result->fetch_assoc();

        if(password_verify($password, $user['password'])){

            $_SESSION['user'] = $user['name'];

            header("Location: dashboard.php");

        } else {
            echo "Wrong Password";
        }

    } else {
        echo "User Not Found";
    }
}
?>

<form method="POST">
    <input type="email" name="email" placeholder="Email" required><br>
    <input type="password" name="password" placeholder="Password" required><br>
    <button type="submit" name="login">Login</button>
</form>

6. Dashboard (dashboard.php)

<?php
session_start();

if(!isset($_SESSION['user'])){
    header("Location: login.php");
    exit;
}

echo "Welcome " . $_SESSION['user'];
?>

<br>
<a href="logout.php">Logout</a>

7. Logout (logout.php)

<?php
session_start();
session_destroy();

header("Location: login.php");
?>

8. Security Improvements (Very Important)

To make this project production-ready:

  • Use prepared statements (prevent SQL injection)

  • Validate email input

  • Add CSRF token

  • Use HTTPS

  • Limit login attempts

  • Use input sanitization


9. Features You Can Add (For Advanced Version)

  • Forgot Password System

  • Email Verification

  • Profile Page

  • Admin Panel

  • Remember Me Feature

  • Role-based Login (Admin/User)


10. Why This Project is Important

This project is important because:

  • It is asked in interviews

  • It is used in real websites

  • It builds your PHP fundamentals

  • It helps you understand authentication systems


Conclusion

You have successfully built a complete Login and Registration System using PHP & MySQL. This is one of the most important beginner-to-intermediate level projects every developer should know.

Practice this project, modify it, and add advanced features to improve your skills.


Blog Growth Tip (Very Important)

To make this post viral:

  • Add GitHub source code download

  • Create YouTube tutorial version

  • Add “Download Project ZIP”

  • Write separate posts:

    • PHP Login System with PDO

    • Secure Login System with AJAX

    • PHP Forgot Password System

  • Share in developer communities

This type of content brings consistent organic traffic + backlinks.

Comments

Popular posts from this blog

Simple PHP Mysql Shopping Cart

How to seperate character from string in php

How to Delete record using PHP Ajax