-
Notifications
You must be signed in to change notification settings - Fork 2
/
change_password.php
52 lines (41 loc) · 1.45 KB
/
change_password.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
<?php
include("dbconnection.php");
$con = dbconnection();
$response = ['success' => 'false'];
if (isset($_POST["email"], $_POST["current_password"], $_POST["new_password"])) {
$email = trim($_POST["email"]);
$current_password = trim($_POST["current_password"]);
$new_password = trim($_POST["new_password"]);
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$response['message'] = 'Invalid email format';
echo json_encode($response);
exit;
}
// Check if user exists and current password is correct
$stmt = $con->prepare("SELECT upassword FROM sign_up WHERE uemail = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($stored_password);
$stmt->fetch();
$stmt->close();
if (!$stored_password || $stored_password !== $current_password) {
$response['message'] = 'Current password is incorrect';
echo json_encode($response);
exit;
}
// Update password
$stmt = $con->prepare("UPDATE sign_up SET upassword = ? WHERE uemail = ?");
$stmt->bind_param("ss", $new_password, $email);
if ($stmt->execute()) {
$response['success'] = 'true';
$response['message'] = 'Password updated successfully';
} else {
$response['message'] = 'Password update failed: ' . $stmt->error;
}
$stmt->close();
} else {
$response['message'] = 'Required fields are missing';
}
echo json_encode($response);
?>