Simple project of How to Create a Registration and Login System(admin access) with PHP,html in MySQL XAMPP phpmyadmin

 After finishing bellow setup your website will look like this , just type http://localhost/user_system/login.php     ( must have to paste project folder inside htdocs folder)


next you can register

After logging you'll see
Admin login



$admin_user = "admin";
$admin_pass = "admin123";

in code here we can see login username and password " admin" "admin123"


after login as admin you can change any users password,other details 


heres the project link from github (you can download whole project as zip or copy code): 

you will get css file also 


CLICK HERE TO DOWNLOAD PROJECT FOLDER




                                                           

If you fullfill  setup and paste your project folder in this path:C:\xampp\htdocs\user_system

 you can run project from localhost (user_system is my project folder)





                                                                     Setup Bellow


At first we have to create db.php for better understand of mysql localhost connection and click start on APACHE and MYSQL button on xampp mysql


next click on Admin button of my sql 

and click on sql to give command of creating database and table ,paste command and click on go button


you can copy the command and save it as 

users.sql 

CREATE DATABASE IF NOT EXISTS user_system;
USE user_system;

CREATE TABLE IF NOT EXISTS users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(100) NOT NULL,
  email VARCHAR(100) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  gender ENUM('Male','Female','Other') NOT NULL,
  address VARCHAR(255) NOT NULL,
  birth_year INT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

After creating database we have to create db.php for connection with mysql

db.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_system";  // create this in phpMyAdmin

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

next we can create login registration pages for admin and users but we have to link the database


register.php

<?php
include 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
    $gender = $_POST['gender'];
    $address = $_POST['address'];
    $birth_year = $_POST['birth_year'];

    // Age validation
    $current_year = date("Y");
    $age = $current_year - $birth_year;

    if ($age < 18) {
        echo "<script>alert('You must be at least 18 years old to register.'); window.location.href='register.php';</script>";
        exit();
    }

    $sql = "INSERT INTO users (username, email, password, gender, address, birth_year)
            VALUES ('$username', '$email', '$password', '$gender', '$address', '$birth_year')";

    if (mysqli_query($conn, $sql)) {
        echo "<script>alert('Registration successful!'); window.location.href='login.php';</script>";
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Register</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="form-container">
    <h2>Register</h2>
   <form action="register.php" method="POST">
  <input type="text" name="username" placeholder="Username" required>
  <input type="email" name="email" placeholder="Email" required>
  <input type="password" name="password" placeholder="Password" required>
  <select name="gender" required>
    <option value="">Select Gender</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
    <option value="Other">Other</option>
  </select>

  <input type="text" name="address" placeholder="Address" required>
  <input type="number" name="birth_year" placeholder="Birth Year (e.g. 2005)" required>

  <button type="submit">Register</button>
</form>

</div>
</body>
</html>

login.php
<?php
session_start();
include 'db.php';

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

    $stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        if (password_verify($password, $user['password'])) {
            $_SESSION['username'] = $user['username'];
            $_SESSION['email'] = $user['email'];
            $_SESSION['role'] = 'user';
            header("Location: user_dashboard.php");
            exit;
        } else {
            echo "<script>alert('Invalid password');</script>";
        }
    } else {
        echo "<script>alert('User not found');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User Login</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="form-container">
    <h2>User Login</h2>
    <form method="POST">
        <input type="email" name="email" placeholder="Email" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit">Login</button>
        <p>No account? <a href="register.php">Register</a></p>
    </form>
    <p><a href="admin_login.php">Admin Login</a></p>
</div>
</body>
</html>

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

dashboard.php
<?php
session_start();
include 'db.php';

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

$result = $conn->query("SELECT * FROM users ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Dashboard</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="dashboard">
    <h2>Welcome, <?php echo $_SESSION['username']; ?> 👋</h2>
    <a href="logout.php" class="logout-btn">Logout</a>

    <h3>Registered Users</h3>
    <table>
        <tr>
            <th>ID</th>
            <th>Username</th>
            <th>Email</th>
            <th>Gender</th>
            <th>Created At</th>
        </tr>
        <?php while ($row = $result->fetch_assoc()) { ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['username']; ?></td>
                <td><?php echo $row['email']; ?></td>
                <td><?php echo $row['gender']; ?></td>
                <td><?php echo $row['created_at']; ?></td>
            </tr>
        <?php } ?>
    </table>
</div>
</body>
</html>

admin_login.php
<?php
session_start();

$admin_user = "admin";
$admin_pass = "admin123"; // default admin credentials

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

    if ($username === $admin_user && $password === $admin_pass) {
        $_SESSION['admin'] = $admin_user;
        header("Location: admin_dashboard.php");
        exit;
    } else {
        echo "<script>alert('Invalid admin credentials');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Login</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="form-container">
    <h2>Admin Login</h2>
    <form method="POST">
        <input type="text" name="username" placeholder="Admin Username" required>
        <input type="password" name="password" placeholder="Admin Password" required>
        <button type="submit">Login</button>
        <p><a href="login.php">Back to User Login</a></p>
    </form>
</div>
</body>
</html>


admin_dashboard.php
<?php
session_start();
include 'db.php';

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

if (isset($_GET['msg']) && $_GET['msg'] == 'updated') {
    echo "<div style='background:#d4edda;color:#155724;padding:10px;border-radius:8px;margin:15px auto;text-align:center;width:90%;max-width:900px;'>
            ✅ User details updated successfully!
          </div>";
}

// ✅ Correct delete logic
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $conn->query("DELETE FROM users WHERE id = $id");
    header("Location: admin_dashboard.php");
    exit;
}

$result = $conn->query("SELECT * FROM users ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Dashboard</title>
<link rel="stylesheet" href="style.css">
<style>
body {
  background: #f2f5f9;
  font-family: Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}

/* Header Bar */
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #ffffff;
  padding: 15px 25px;
  border-radius: 12px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.1);
  width: 90%;
  max-width: 900px;
  margin-top: 30px;
}
.header-bar h2 {
  color: #333;
  margin: 0;
}
.logout-btn {
  background: #dc3545;
  color: #fff;
  padding: 8px 14px;
  border-radius: 8px;
  text-decoration: none;
  transition: background 0.3s ease;
}
.logout-btn:hover {
  background: #a71d2a;
}

/* Table Wrapper */
.table-wrapper {
  background: #fff;
  padding: 25px;
  margin-top: 30px;
  border-radius: 15px;
  box-shadow: 0 4px 10px rgba(0,0,0,0.1);
  width: 90%;
  max-width: 900px;
  overflow-x: auto;
}

/* Table Styling */
table {
  width: 100%;
  border-collapse: collapse;
}
table th, table td {
  border: 1px solid #ddd;
  padding: 10px;
  text-align: center;
}
table th {
  background: #007bff;
  color: #fff;
}
.change-btn, .delete-btn {
  padding: 6px 10px;
  border-radius: 6px;
  color: #fff;
  text-decoration: none;
  display: inline-block;
}
.change-btn {
  background: #28a745;
}
.change-btn:hover {
  background: #1e7e34;
}
.delete-btn {
  background: #dc3545;
}
.delete-btn:hover {
  background: #a71d2a;
}
</style>
</head>
<body>

<div class="header-bar">
  <h2>👑 Admin Dashboard</h2>
  <a href="admin_logout.php" class="logout-btn">Logout</a>
</div>

<div class="table-wrapper">
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Username</th>
        <th>Email</th>
        <th>Gender</th>
        <th>Address</th>
        <th>Birth Year</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
      <?php while ($row = mysqli_fetch_assoc($result)): ?>
        <tr>
          <td><?= $row['id'] ?></td>
          <td><?= htmlspecialchars($row['username']) ?></td>
          <td><?= htmlspecialchars($row['email']) ?></td>
          <td><?= htmlspecialchars($row['gender']) ?></td>
          <td><?= htmlspecialchars($row['address']) ?></td>
          <td><?= htmlspecialchars($row['birth_year']) ?></td>
          <td>
            <a href="edit_user.php?id=<?= $row['id'] ?>" class="change-btn">Edit</a>
            <a href="admin_dashboard.php?delete=<?= $row['id'] ?>" class="delete-btn" onclick="return confirm('Delete this user?')">Delete</a>
          </td>
        </tr>
      <?php endwhile; ?>
    </tbody>
  </table>
</div>

</body>
</html>


admin_logout.php
<?php
session_start();
session_unset();    // clear session variables
session_destroy();  // destroy session completely

// 🔁 Redirect admin back to login page
header("Location: admin_login.php");
exit;
?>


change_password.php
<?php
session_start();
include 'db.php';

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = $_SESSION['email'];
    $newpass = password_hash($_POST['new_password'], PASSWORD_DEFAULT);
    $conn->query("UPDATE users SET password='$newpass' WHERE email='$email'");
    echo "<script>alert('Password updated successfully!'); window.location='user_dashboard.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Password</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="form-container">
    <h2>Change Password</h2>
    <form method="POST">
        <input type="password" name="new_password" placeholder="Enter new password" required>
        <button type="submit">Update Password</button>
    </form>
</div>
</body>
</html>

edit_user.php
<?php
include 'db.php';

// ✅ Sanitize and validate ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid user ID.");
}

$id = intval($_GET['id']);

// ✅ Fetch the user to edit
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$user = mysqli_fetch_assoc($result);

if (!$user) {
    die("User not found.");
}

// ✅ Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $gender = mysqli_real_escape_string($conn, $_POST['gender']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $birth_year = intval($_POST['birth_year']);

    // ✅ Age validation
    $current_year = date("Y");
    $age = $current_year - $birth_year;

    if ($age < 18) {
        echo "<script>alert('User must be at least 18 years old.');</script>";
    } else {
        $update = "
            UPDATE users 
            SET 
                username = '$username',
                gender = '$gender',
                address = '$address',
                birth_year = '$birth_year'
            WHERE id = $id
        ";
        
        if (mysqli_query($conn, $update)) {
            header("Location: admin_dashboard.php?msg=updated");
            exit();
        } else {
            echo "Error updating user: " . mysqli_error($conn);
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Edit User</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="form-container">
    <h2>Edit User</h2>
    <form method="POST">
      <input type="text" name="username" value="<?= htmlspecialchars($user['username']) ?>" required>
      
      <select name="gender" required>
        <option value="Male" <?= $user['gender'] == 'Male' ? 'selected' : '' ?>>Male</option>
        <option value="Female" <?= $user['gender'] == 'Female' ? 'selected' : '' ?>>Female</option>
        <option value="Other" <?= $user['gender'] == 'Other' ? 'selected' : '' ?>>Other</option>
      </select>

      <input type="text" name="address" value="<?= htmlspecialchars($user['address']) ?>" placeholder="Address" required>

      <input type="number" name="birth_year" value="<?= htmlspecialchars($user['birth_year']) ?>" placeholder="Birth Year" required>

      <button type="submit">Save Changes</button>
      <a href="admin_dashboard.php" class="logout-btn" style="display:inline-block;width:auto;margin-top:10px;">Cancel</a>
    </form>
  </div>
</body>
</html>


Post a Comment

0 Comments