-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResetPasswordHandle.php
55 lines (49 loc) · 2.22 KB
/
ResetPasswordHandle.php
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
<?php
require('./db_conn.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$token = $_POST["token"];
$newPassword = $_POST["newPassword"];
$confirmPassword = $_POST["confirmPassword"];
// Validate inputs and password strength
if (empty($token) || empty($newPassword) || empty($confirmPassword)) {
echo "All fields are required.";
} elseif (strlen($newPassword) < 8) {
echo "Password must be at least 8 characters long.";
} elseif (!preg_match('/[A-Z]/', $newPassword)) {
echo "Password must contain at least one capital letter.";
} elseif (!preg_match('/[a-z]/', $newPassword)) {
echo "Password must contain at least one lowercase letter.";
} elseif (!preg_match('/[0-9]/', $newPassword)) {
echo "Password must contain at least one digit.";
} elseif (!preg_match('/[^a-zA-Z\d]/', $newPassword)) {
echo "Password must contain at least one special character.";
} elseif ($newPassword !== $confirmPassword) {
echo "Passwords do not match.";
} else {
// Hash the new password before storing it
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
// Check if the token exists
$checkTokenQuery = "SELECT ResetToken FROM profitedge_users WHERE ResetToken = ?";
$checkTokenStmt = $mysqli->prepare($checkTokenQuery);
$checkTokenStmt->bind_param('s', $token);
$checkTokenStmt->execute();
$checkTokenResult = $checkTokenStmt->get_result();
if ($checkTokenResult->num_rows === 0) {
echo "Invalid token.";
} else {
// Proceed with password change
$updatePasswordQuery = "UPDATE profitedge_users SET Password = ?, ResetToken = NULL WHERE ResetToken = ?";
$updatePasswordStmt = $mysqli->prepare($updatePasswordQuery);
$updatePasswordStmt->bind_param('ss', $hashedPassword, $token);
if ($updatePasswordStmt->execute()) {
echo "Password has been successfully changed.";
} else {
echo "Error updating the password.";
}
$updatePasswordStmt->close();
}
$checkTokenStmt->close();
}
} else {
echo "Invalid request.";
}