-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_password.php
More file actions
68 lines (54 loc) · 1.65 KB
/
change_password.php
File metadata and controls
68 lines (54 loc) · 1.65 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
<?php
session_start();
include "db.php";
// Must be logged in
if (!isset($_SESSION['user'])) {
header("Location: login.php");
exit;
}
$message = "";
$username = $_SESSION['user'];
if (isset($_POST['change'])) {
$current = $_POST['current_password'];
$new = $_POST['new_password'];
// Fetch current user
$result = mysqli_query($conn,
"SELECT password FROM users WHERE username='$username' LIMIT 1"
);
$user = mysqli_fetch_assoc($result);
if ($user && password_verify($current, $user['password'])) {
$newHash = password_hash($new, PASSWORD_DEFAULT);
mysqli_query($conn,
"UPDATE users SET password='$newHash' WHERE username='$username'"
);
$message = "Password changed successfully";
} else {
$message = "Current password is incorrect";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Change Password</title>
<link rel="stylesheet" href="login.css">
</head>
<body>
<div class="login-wrapper">
<div class="login-card">
<h2>Change Password</h2>
<?php if ($message): ?>
<div class="error"><?= $message ?></div>
<?php endif; ?>
<form method="POST">
<input type="password" name="current_password" placeholder="Current Password" required>
<input type="password" name="new_password" placeholder="New Password" required>
<button name="change">Update Password</button>
</form>
<p style="margin-top:15px;">
<a href="dashboard.php">Back to Dashboard</a>
</p>
</div>
</div>
</body>
</html>