-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforgot_password.php
More file actions
137 lines (134 loc) · 5.11 KB
/
forgot_password.php
File metadata and controls
137 lines (134 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
session_start();
include "connection.php";
$step = 1;
$error_message = "";
$success_message = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['email'])) {
// Step 1: Verify email exists
$email = trim($_POST['email']);
if (empty($email)) {
$error_message = "Please enter your email.";
} else {
$stmt = $db->prepare("SELECT id FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 1) {
$step = 2;
$_SESSION['reset_email'] = $email;
} else {
$error_message = "Email not found.";
}
$stmt->close();
}
} elseif (isset($_POST['password']) && isset($_POST['confirm_password'])) {
// Step 2: Update password
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if (empty($password) || empty($confirm_password)) {
$error_message = "Please fill in all password fields.";
$step = 2;
} elseif ($password !== $confirm_password) {
$error_message = "Passwords do not match.";
$step = 2;
} elseif (!isset($_SESSION['reset_email'])) {
$error_message = "Session expired. Please start over.";
$step = 1;
} else {
$email = $_SESSION['reset_email'];
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("UPDATE users SET password_hash = ? WHERE email = ?");
$stmt->bind_param("ss", $password_hash, $email);
if ($stmt->execute()) {
$success_message = "Password updated successfully. You can now <a href='student_login.php'>login</a>.";
unset($_SESSION['reset_email']);
$step = 3;
} else {
$error_message = "Error updating password. Please try again.";
$step = 2;
}
$stmt->close();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Forgot Password | Booker Library</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 6px 20px rgba(0,0,0,0.1);
width: 100%;
max-width: 400px;
}
h2 {
margin-bottom: 20px;
text-align: center;
}
.error-message {
color: #dc3545;
margin-bottom: 15px;
text-align: center;
}
.success-message {
color: #28a745;
margin-bottom: 15px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<?php if ($step === 1): ?>
<h2>Forgot Password</h2>
<?php if ($error_message): ?>
<div class="error-message"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<form method="post" action="forgot_password.php">
<div class="form-group">
<label for="email">Enter your registered email address</label>
<input type="email" class="form-control" id="email" name="email" required />
</div>
<button type="submit" class="btn btn-primary btn-block">Next</button>
<a href="student_login.php" class="btn btn-link btn-block">Back to Login</a>
</form>
<?php elseif ($step === 2): ?>
<h2>Reset Password</h2>
<?php if ($error_message): ?>
<div class="error-message"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<form method="post" action="forgot_password.php">
<div class="form-group">
<label for="password">New Password</label>
<input type="password" class="form-control" id="password" name="password" required />
</div>
<div class="form-group">
<label for="confirm_password">Confirm New Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required />
</div>
<button type="submit" class="btn btn-primary btn-block">Reset Password</button>
</form>
<?php elseif ($step === 3): ?>
<h2>Password Reset Successful</h2>
<div class="success-message"><?php echo $success_message; ?></div>
<?php endif; ?>
</div>
</body>
</html>