Admin and user login in php mysql. In this tutorial, you will learn step by step how to create admin and user login in php mysql.
If you are creating any web application in PHP MySQL and want to add admin and user login functionality in that web application. So, you can download and use the completely free source code of admin and user login in php mysql.
User And Admin Login System In PHP MySQL
By using the following steps, you can create admin and user login page in PHP MySQL:
- Step 1: Create the Database and Table
- Step 2: Setting up the Database
- Step 3: Create Admin and User Login Page
- Step 4: Create Admin Dashboard Page
- Step 5: Create User Dashboard Page
- Step 6: Create Logout PHP File
Step 1: Create the Database and Table
First of all, open your phpmyadmin and execute 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: Setting up the Database
Now, open your php project and create one file name the config.php
file. And then you need to add the following code into 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
Next, you need to create one login page, which is used to login for admin and user. So, create a file named “login.php” and add the following code:
<?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
Once you have created the login.php file, now you need to create the admin dashboard php file. So that whenever the admin login then after the admin can be redirected to the admin dashboard page. So create a file named “admin.php” for the admin dashboard and add the following code:
<?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
Next, you need to create the user dashboard php file. So that whenever the user login then after the user can be redirected to the user dashboard page. So Create a file named “user.php” for the regular user dashboard and add the following code:
<?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
When both the user and the admin are logged in, they have to be given the option to logout in dashboards. So, Create a file named “logout.php” for the logout functionality and add the following code:
<?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.
And also, you can use the free source code of admin and user login in php mysql as starting point for building a secure and user-friendly authentication system for your web application.