-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_change_password.php
More file actions
69 lines (64 loc) · 2.95 KB
/
Copy pathadmin_change_password.php
File metadata and controls
69 lines (64 loc) · 2.95 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
<?php
include 'includes/db.php';
include 'includes/header.php';
// Only allow admin
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header('Location: login.php?type=admin');
exit();
}
$change_password_success = false;
if (isset($_POST['change_admin_password'])) {
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_new_password = $_POST['confirm_new_password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$admin = $stmt->fetch(PDO::FETCH_ASSOC);
if ($admin && password_verify($current_password, $admin['password'])) {
if ($new_password === $confirm_new_password) {
if (strlen($new_password) >= 6) {
$hashed_new_password = password_hash($new_password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
$stmt->execute([$hashed_new_password, $_SESSION['user_id']]);
$change_password_message = "Password changed successfully.";
$change_password_success = true;
} else {
$change_password_message = "New password must be at least 6 characters.";
}
} else {
$change_password_message = "New passwords do not match.";
}
} else {
$change_password_message = "Current password is incorrect.";
}
}
?>
<div class="main-content">
<?php include 'includes/sidebar.php'; ?>
<div class="content-sections">
<section class="content-section">
<h2>Change Password</h2>
<form method="POST" style="max-width:400px;">
<div class="form-group">
<label class="form-label">Current Password</label>
<input type="password" name="current_password" class="form-input" required>
</div>
<div class="form-group">
<label class="form-label">New Password</label>
<input type="password" name="new_password" class="form-input" required>
</div>
<div class="form-group">
<label class="form-label">Confirm New Password</label>
<input type="password" name="confirm_new_password" class="form-input" required>
</div>
<button type="submit" name="change_admin_password" class="btn">Change Password</button>
</form>
<?php if (isset($change_password_message)): ?>
<div style="margin-top:1rem; color:<?php echo $change_password_success ? '#155724' : '#a00'; ?>; background:<?php echo $change_password_success ? '#e3fbe3' : '#ffe3e3'; ?>; padding:1rem; border-radius:8px;">
<?php echo htmlspecialchars($change_password_message); ?>
</div>
<?php endif; ?>
</section>
</div>
</div>
<?php include 'includes/footer.php'; ?>