-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathforget-password.php
More file actions
287 lines (253 loc) · 11.1 KB
/
Copy pathforget-password.php
File metadata and controls
287 lines (253 loc) · 11.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
// Add this code near the beginning of forget-password.php
if (isset($_SESSION['pwd-error'])) {
$error = $_SESSION['pwd-error'];
unset($_SESSION['pwd-error']);
}
session_start();
require_once 'config/db.php';
// Function to generate secure random token
function generateToken($length = 64)
{
return bin2hex(random_bytes($length / 2));
}
// Function to generate OTP
function generateOTP()
{
return sprintf("%06d", mt_rand(0, 999999));
}
// Function to generate and store new OTP
function generateAndStoreOTP($conn, $email)
{
$otp = generateOTP();
$token = generateToken();
$expiry = date('Y-m-d H:i:s', strtotime('+1 minute')); // Set expiry to 1 minute
// First, invalidate all previous OTPs for this email
$stmt = $conn->prepare("UPDATE password_resets SET is_expired = 1 WHERE email = ? AND is_expired = 0");
$stmt->bind_param("s", $email);
$stmt->execute();
// Store new OTP
$stmt = $conn->prepare("INSERT INTO password_resets (email, otp, token, expiry) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $email, $otp, $token, $expiry);
if ($stmt->execute()) {
$_SESSION['pwd-resend-timer'] = time() + 60;
$_SESSION['pwd-otp-expiry'] = strtotime($expiry);
$_SESSION['pwd-timer-start'] = time();
return true;
}
return false;
}
$error = '';
$success = '';
$step = 1;
$email = '';
// Restore email from session if available
if (isset($_SESSION['pwd-reset-email'])) {
$email = $_SESSION['pwd-reset-email'];
if (isset($_SESSION['pwd-otp-step']) && $_SESSION['pwd-otp-step'] === true) {
$step = 2;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['email']) && !empty($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
// Check if user exists
$stmt = $conn->prepare("SELECT email FROM users WHERE email = ? AND status = 'verified'");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
if (generateAndStoreOTP($conn, $email)) {
$success = "OTP has been generated successfully! OTP will expire in 1 minute.";
$step = 2;
$_SESSION['reset_email'] = $email;
$_SESSION['otp_step'] = true;
} else {
$error = "Error generating OTP. Please try again.";
}
} else {
$error = "Email address not found!";
}
} elseif (isset($_POST['otp'])) {
$entered_otp = $_POST['otp'];
$email = $_POST['user_email'];
// Verify OTP
$current_time = date('Y-m-d H:i:s');
$stmt = $conn->prepare("
SELECT token, expiry
FROM password_resets
WHERE email = ?
AND otp = ?
AND used = 0
AND is_expired = 0
AND expiry > ?
ORDER BY created_at DESC
LIMIT 1
");
$stmt->bind_param("sss", $email, $entered_otp, $current_time);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
// Mark OTP as used
$update_stmt = $conn->prepare("UPDATE password_resets SET used = 1 WHERE email = ? AND otp = ?");
$update_stmt->bind_param("ss", $email, $entered_otp);
$update_stmt->execute();
// Clear OTP-related session variables
unset($_SESSION['resend_timer']);
unset($_SESSION['otp_step']);
unset($_SESSION['otp_expiry']);
unset($_SESSION['current_otp']);
header("Location: reset-password.php?token=" . $row['token']);
exit();
} else {
// Check if OTP exists but expired
$stmt = $conn->prepare("
SELECT expiry
FROM password_resets
WHERE email = ?
AND otp = ?
AND used = 0
ORDER BY created_at DESC
LIMIT 1
");
$stmt->bind_param("ss", $email, $entered_otp);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
if (strtotime($row['expiry']) <= strtotime($current_time)) {
$error = "OTP has expired! Please request a new OTP.";
}
} else {
$error = "Invalid OTP! Please check and try again.";
}
$step = 2;
}
} elseif (isset($_POST['resend_otp'])) {
$email = $_POST['user_email'];
if (!isset($_SESSION['resend_timer']) || time() >= $_SESSION['resend_timer']) {
if (generateAndStoreOTP($conn, $email)) {
$success = "New OTP has been generated and sent successfully! OTP will expire in 1 minute.";
$step = 2;
} else {
$error = "Error generating new OTP. Please try again.";
}
} else {
$error = "Please wait before requesting a new OTP.";
$step = 2;
}
}
}
// Calculate remaining times
$resendTimeRemaining = isset($_SESSION['resend_timer']) ? max(0, $_SESSION['resend_timer'] - time()) : 0;
$otpExpiryRemaining = isset($_SESSION['otp_expiry']) ? max(0, $_SESSION['otp_expiry'] - time()) : 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reset Password - Annapurna Hotel and Restaurant</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
<link rel="stylesheet" href="assets/css/forget-password.css">
</head>
<body>
<div class="pwd-container">
<div class="pwd-form-container">
<div class="pwd-logo">
<img src="logo.png" alt="Annapurna Hotel Logo">
</div>
<h1>Reset Password</h1>
<?php if ($error): ?>
<div class="pwd-alert pwd-alert-error">
<ion-icon name="alert-circle"></ion-icon>
<?php echo $error; ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="pwd-alert pwd-alert-success">
<ion-icon name="checkmark-circle"></ion-icon>
<?php echo $success; ?>
</div>
<?php endif; ?>
<div id="pwd-step1Form" <?php echo $step == 2 ? 'style="display:none;"' : ''; ?>>
<form id="pwd-emailForm" method="POST" action="">
<div class="pwd-form-group">
<label for="pwd-email">
<ion-icon name="mail"></ion-icon>
Email Address
</label>
<input type="email" id="pwd-email" name="email"
value="<?php echo htmlspecialchars($email); ?>"
required placeholder="Enter your email address">
</div>
<div class="pwd-button-group">
<button type="submit" class="pwd-btn pwd-btn-primary">
<ion-icon name="paper-plane"></ion-icon>
Get OTP
</button>
<a href="login.php" class="pwd-text-link">
<ion-icon name="arrow-back"></ion-icon>
Back to Login
</a>
</div>
</form>
</div>
<!-- Step 2 form with modified email display and timer -->
<div id="pwd-step2Form" <?php echo $step == 1 ? 'style="display:none;"' : ''; ?>>
<div class="pwd-email-display">
<span>Your entered email is: <?php echo htmlspecialchars($email); ?></span>
<a href="javascript:void(0)" class="pwd-text-link" onclick="changeEmail()">Change</a>
</div>
<div class="pwd-timer-container">
<div class="pwd-otp-timer">
<ion-icon name="timer-outline"></ion-icon>
OTP expires in: <span id="pwd-otpTimer">00:00</span>
</div>
</div>
<form id="pwd-otpForm" method="POST" action="">
<input type="hidden" name="user_email" value="<?php echo htmlspecialchars($email); ?>">
<div class="pwd-form-group">
<label for="pwd-otp">Enter OTP</label>
<div class="pwd-otp-container">
<input type="text" maxlength="1" class="pwd-otp-input" data-index="1">
<input type="text" maxlength="1" class="pwd-otp-input" data-index="2">
<input type="text" maxlength="1" class="pwd-otp-input" data-index="3">
<input type="text" maxlength="1" class="pwd-otp-input" data-index="4">
<input type="text" maxlength="1" class="pwd-otp-input" data-index="5">
<input type="text" maxlength="1" class="pwd-otp-input" data-index="6">
</div>
<input type="hidden" name="otp" id="pwd-otpFinal">
</div>
<div class="pwd-button-group">
<button type="submit" class="pwd-btn pwd-btn-primary">
<ion-icon name="checkmark-circle"></ion-icon>
Verify OTP
</button>
</div>
</form>
<!-- Resend OTP button will be shown only after OTP expires -->
<div class="pwd-resend-container" style="display: none;">
<form method="POST" action="" class="pwd-resend-form">
<input type="hidden" name="user_email" value="<?php echo htmlspecialchars($email); ?>">
<input type="hidden" name="resend_otp" value="1">
<button type="submit" class="pwd-text-link pwd-btn-resend">
<ion-icon name="refresh-circle"></ion-icon>
Resend OTP
</button>
</form>
</div>
</div>
</div>
</div>
<script>
// Initialize PHP variables for JavaScript
const initialResendTimeRemaining = <?php echo isset($_SESSION['pwd-resend-timer']) ? max(0, $_SESSION['pwd-resend-timer'] - time()) : 0; ?>;
const initialOtpExpiryRemaining = <?php echo isset($_SESSION['pwd-otp-expiry']) ? max(0, $_SESSION['pwd-otp-expiry'] - time()) : 0; ?>;
</script>
<script src="assets/js/forgotpwd.js"></script>
</body>
</html>