forked from NightingaleNath/leave_portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_pass_function.php
More file actions
45 lines (37 loc) · 1.57 KB
/
Copy pathreset_pass_function.php
File metadata and controls
45 lines (37 loc) · 1.57 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
<?php
include('includes/config.php');
include('includes/session.php');
if (!isset($_SESSION['semail'])) {
header('Location: index.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] === 'reset_password') {
$newPassword = $_POST['new_password'];
$confirmPassword = $_POST['confirm_password'];
$email = $_SESSION['semail'];
if (empty($newPassword) || empty($confirmPassword)) {
$response = array('status' => 'error', 'message' => 'Please fill in all fields');
echo json_encode($response);
exit();
}
if ($newPassword !== $confirmPassword) {
$response = array('status' => 'error', 'message' => 'Passwords do not match');
echo json_encode($response);
exit();
}
$hashedPassword = md5($newPassword); // Hash the new password
// Update the password and set password_reset to true
$stmt = mysqli_prepare($conn, "UPDATE tblemployees SET password = ?, password_reset = 1 WHERE email_id = ?");
mysqli_stmt_bind_param($stmt, "ss", $hashedPassword, $email);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_affected_rows($stmt) > 0) {
// Automatically log the user in after password reset
$_SESSION['password_reset'] = true; // Set password_reset session variable
$response = array('status' => 'success', 'message' => 'Password reset successfully', 'role' => $_SESSION['srole']);
} else {
$response = array('status' => 'error', 'message' => 'Failed to reset password');
}
echo json_encode($response);
exit();
}
?>