-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForgotPassword.php
More file actions
174 lines (155 loc) · 7.01 KB
/
ForgotPassword.php
File metadata and controls
174 lines (155 loc) · 7.01 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
session_start();
include 'db.php';
$error_message = '';
$success_message = '';
$show_security_answer = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!isset($_POST['security_step'])) {
// First step: Verify email and show security question
$email = trim($_POST['email']);
$stmt = $con->prepare("SELECT username, security_question FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
$_SESSION['reset_email'] = $email;
$show_security_answer = true;
$security_question = $user['security_question'];
} else {
$error_message = "No user found with that email address.";
}
$stmt->close();
} elseif (isset($_POST['security_step']) && $_POST['security_step'] == 'verify_answer') {
// Second step: Verify security answer
$email = $_SESSION['reset_email'];
$security_answer = $_POST['security_answer'];
$stmt = $con->prepare("SELECT security_answer FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
// Verify security answer
if (password_verify($security_answer, $user['security_answer'])) {
// Security answer is correct, allow password reset
$_SESSION['security_verified'] = true;
$show_security_answer = false;
} else {
$error_message = "Incorrect security answer. Please try again.";
}
} else {
$error_message = "User not found.";
}
$stmt->close();
} elseif (isset($_POST['security_step']) && $_POST['security_step'] == 'reset_password') {
// Third step: Reset password
if (!$_SESSION['security_verified']) {
$error_message = "Security verification required.";
} else {
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
$email = $_SESSION['reset_email'];
// Validate new password
if (empty($new_password) || empty($confirm_password)) {
$error_message = "Please enter both password fields.";
} elseif ($new_password !== $confirm_password) {
$error_message = "Passwords do not match.";
} elseif (!preg_match("/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_]).{8,}$/", $new_password)) {
$error_message = "Password must be at least 8 characters, with at least one uppercase letter, one lowercase letter, one number, and one special character.";
} else {
// Hash the new password
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
// Update password in database
$stmt = $con->prepare("UPDATE users SET password = ? WHERE email = ?");
$stmt->bind_param("ss", $hashed_password, $email);
if ($stmt->execute()) {
// Clear session variables
unset($_SESSION['reset_email']);
unset($_SESSION['security_verified']);
$success_message = "Password reset successfully. You can now log in.";
} else {
$error_message = "Error updating password. Please try again.";
}
$stmt->close();
}
}
}
}
$con->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forgot Password</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./Styles/Login.css">
<style>
.success-message {
background: rgba(82, 255, 88, 0.1);
color: green;
padding: 12px;
border-radius: 12px;
margin-bottom: 20px;
font-size: 14px;
text-align: center;
border: 1px solid rgba(82, 255, 88, 0.2);
}
</style>
</head>
<body>
<div class="login-container">
<form action="ForgotPassword.php" method="POST">
<h1>Reset Password</h1>
<?php if (!empty($error_message)): ?>
<div class="error-message">
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php endif; ?>
<?php if (!empty($success_message)): ?>
<div class="success-message">
<?php echo htmlspecialchars($success_message); ?>
</div>
<?php endif; ?>
<?php if (!$show_security_answer && !isset($_SESSION['security_verified'])): ?>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="Enter your email" required>
</div>
<input type="submit" value="Verify Email">
<?php endif; ?>
<?php if ($show_security_answer): ?>
<input type="hidden" name="security_step" value="verify_answer">
<div class="form-group">
<label>Security Question</label>
<input type="text" value="<?php echo htmlspecialchars($security_question); ?>" readonly>
</div>
<div class="form-group">
<label for="security_answer">Security Answer</label>
<input type="text" id="security_answer" name="security_answer" placeholder="Enter your security answer" required>
</div>
<input type="submit" value="Verify Answer">
<?php endif; ?>
<?php if (isset($_SESSION['security_verified']) && $_SESSION['security_verified']): ?>
<input type="hidden" name="security_step" value="reset_password">
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" id="new_password" name="new_password" placeholder="Enter new password" required>
</div>
<div class="form-group">
<label for="confirm_password">Confirm New Password</label>
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm new password" required>
</div>
<input type="submit" value="Reset Password">
<?php endif; ?>
<div class="register-link">
<span>Remember your password? </span>
<a href="login.php">Back to Login</a>
</div>
</form>
</div>
</body>
</html>