Forgot password
New login page with forgot password link...
login.php
<?php
// Database Connection
include 'db/db.php';
// Handle Form Submission
if (isset($_POST['login'])) {
$email = mysqli_real_escape_string($conn,
$_POST['email']);
$password = $_POST['password'];
// Query to check if the user exists
$sql = "SELECT * FROM users WHERE
email = '$email'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_assoc($result);
// Verify password
if (password_verify($password,
$user['password'])) {
session_start();
$_SESSION['user_id']
= $user['id'];
$_SESSION['user_name']
= $user['name'];
$_SESSION['user_email']
= $user['email'];
$_SESSION['user_role']
= $user['role']; // Store user role in session
// Redirect
based on role
if ($_SESSION['user_role']
== 'admin') {
header("Location: admin_dashboard.php"); // Redirect to admin
dashboard
} else {
header("Location: user_dashboard.php"); // Redirect to user
dashboard
}
exit;
} else {
$message = "Incorrect
Password!";
}
} else {
$message = "No user
found with this email!";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login - EventMaster</title>
<meta name="viewport" content="width=device-width,
initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet">
<!-- AOS Animation -->
<link href="https://unpkg.com/aos@2.3.1/dist/aos.css"
rel="stylesheet">
<style>
body {
background: linear-gradient(45deg,
#8e2de2, #4a00e0);
display: flex;
align-items:
center;
justify-content:
center;
height: 100vh;
margin: 0;
}
.card {
border-radius:
15px;
box-shadow: 0
10px 30px rgba(0,0,0,0.3);
}
</style>
</head>
<body>
<div class="card p-4" style="width:
350px;" data-aos="zoom-in">
<h3 class="text-center
mb-3">Login 🚀</h3>
<?php if (!empty($message)): ?>
<div class="alert
alert-danger"><?php echo $message; ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label>Email:</label>
<input type="email"
name="email" class="form-control" required />
</div>
<div class="mb-3">
<label>Password:</label>
<input type="password"
name="password" class="form-control" required />
</div>
<button type="submit"
name="login" class="btn btn-primary w-100">Login</button>
</form>
<p class="mt-3 text-center">Don't
have an account? <a href="register.php">Register</a></p>
<p class="mt-2 text-center"><a
href="forgot_password.php">Forgot Password?</a></p>
<!-- Forgot password link -->
</div>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- AOS JS -->
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>
AOS.init({ duration: 1000 });
</script>
</body>
</html>
…
Forgot_password.php
<?php
// Database Connection
include 'db/db.php';
$message = "";
// Handle password reset form
if (isset($_POST['reset_password'])) {
$email = $_POST['email'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
// Check if new passwords match
if ($new_password !== $confirm_password)
{
$message = "New
password and confirm password do not match!";
} else {
// Check if email exists
$sql = "SELECT * FROM
users WHERE email = '$email'";
$result = mysqli_query($conn,
$sql);
if (mysqli_num_rows($result)
> 0) {
// Hash new
password
$hashed_password
= password_hash($new_password, PASSWORD_BCRYPT);
// Update
password
$update_sql
= "UPDATE users SET password = '$hashed_password' WHERE email = '$email'";
if (mysqli_query($conn,
$update_sql)) {
$message = "Password updated successfully!";
} else {
$message = "Error updating password!";
}
} else {
$message = "No
user found with that email address!";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reset Password -
EventMaster</title>
<meta name="viewport" content="width=device-width,
initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="card p-4" style="width:
350px; margin: 100px auto;">
<h3 class="text-center
mb-3">Reset Password</h3>
<?php if (!empty($message)): ?>
<div class="alert
alert-info"><?php echo $message; ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label>Email
Address:</label>
<input type="email"
name="email" class="form-control" required />
</div>
<div class="mb-3">
<label>New
Password:</label>
<input type="password"
name="new_password" class="form-control" required />
</div>
<div class="mb-3">
<label>Confirm
New Password:</label>
<input type="password"
name="confirm_password" class="form-control" required />
</div>
<button type="submit"
name="reset_password" class="btn btn-primary w-100">Reset
Password</button>
</form>
<p class="mt-3 text-center"><a
href="login.php">Back to Login</a></p>
</div>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>