Admin and User Login in PHP with MySQL

In this tutorial, you will learn step by step how to create admin and user login in php with MySQL using session.

How to Create Login Page for Admin and User in PHP Mysql

Here are the steps to create administrator and user login pages:

Step 1: Create the Database and Table

Run the following sql queries to create database and tables for your admin and user login in php mysql system:

CREATE DATABASE login_system;
USE login_system;

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin', 'user') NOT NULL
);

Step 2: Set up the Database

Create config.php file in project folder and, add the following code to it:

<?php
// Database configuration
$host = "localhost";
$username = "your_username";
$password = "your_password";
$database = "login_system";

// Create a database connection
$conn = mysqli_connect($host, $username, $password, $database);

// Check if the connection was successful
if (!$conn) {
    die("Database connection failed: " . mysqli_connect_error());
}
?>

Step 3: Create Admin and User Login Page

Create login.php file and create login page for admin and user in it:

<?php
session_start();
include('config.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_array($result);

    if (mysqli_num_rows($result) == 1) {
        $_SESSION['user_id'] = $row['id'];
        $_SESSION['role'] = $row['role'];

        if ($row['role'] == 'admin') {
            header("Location: admin.php");
        } else {
            header("Location: user.php");
        }
    } else {
        $error = "Invalid username or password!";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>User And Admin Login System In PHP MySQL Step By Step
-tutsmake.com</title>
</head>
<body>
    <h2>Login</h2>
    <?php if (isset($error)) { echo "<p>$error</p>"; } ?>
    <form method="post" action="">
        <label>Username:</label>
        <input type="text" name="username" required><br>

        <label>Password:</label>
        <input type="password" name="password" required><br>

        <input type="submit" value="Login">
    </form>
</body>
</html>

Step 4: Create Admin Dashboard Page

Create user.php file for showing admin dashboard, add the following code into it:

<?php
session_start();

if ($_SESSION['role'] != 'admin') {
    header("Location: login.php");
}

// Admin-specific functionality and HTML content here
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Dashboard</title>
</head>
<body>
    <h2>Welcome, Admin!</h2>
    <p>Admin dashboard content goes here.</p>
    <a href="logout.php">Logout</a>
</body>
</html>

Step 5: Create User Dashboard Page

Create user.php file for showing user dashboard, and add the following code into it:

<?php
session_start();

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

// User-specific functionality and HTML content here
?>

<!DOCTYPE html>
<html>
<head>
<title>User Dashboard</title>
</head>
<body>
<h2>Welcome, User!</h2>
<p>User dashboard content goes here.</p>
<a href="logout.php">Logout</a>
</body>
</html>

Step 6: Create Logout PHP File

Create a file named “logout.php” for the logout functionality:

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

Conclusion

In this tutorial guide, You have learned how to implement admin and user login systems using PHP and MySQL.

To download and use the completely free source code of admin and user login in php mysql.

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *