-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-reset-password.php
More file actions
70 lines (46 loc) · 1.41 KB
/
Copy pathprocess-reset-password.php
File metadata and controls
70 lines (46 loc) · 1.41 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
<?php
$token = $_POST["token"];
$token_hash = hash("sha256", $token);
require "dbcon.php"; // Make sure "dbcon.php" returns the $mysqli connection
$sql = "SELECT * FROM student_registration
WHERE reset_token = ?";
$stmt = $conn->prepare ($sql);
if (!$stmt) {
die("Prepare failed: " . $mysqli->error);
}
$stmt->bind_param("s", $token_hash);
if (!$stmt->execute()) {
die("Execute failed: " . $stmt->error);
}
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close(); // Close the first prepared statement
if ($user === null) {
die("Token not found");
}
if (strtotime($user["token_expires"]) <= time()) {
die("Token has expired");
}
if (strlen($_POST["password"]) < 8) {
die("Password must be at least 8 characters");
}
if (!preg_match("/[a-z]/i", $_POST["password"])) {
die("Password must contain at least one letter");
}
if (!preg_match("/[0-9]/", $_POST["password"])) {
die("Password must contain at least one number");
}
if ($_POST["password"] !== $_POST["password_confirmation"]) {
die("Passwords must match");
}
$password = $_POST['password'];
$sql = "UPDATE student_registration
SET password = ?,
reset_token = NULL,
token_expires = NULL
WHERE s_id = ?";
$stmt =$conn->prepare($sql);
$stmt->bind_param("ss", $password, $user["s_id"]);
$stmt->execute();
echo "Password updated. You can now login.";
?>