-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_password.php
More file actions
101 lines (87 loc) · 4.86 KB
/
Copy pathchange_password.php
File metadata and controls
101 lines (87 loc) · 4.86 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
require_once("includes/auth.php");
require_login();
include("config.php");
$errors = [];
$success = false;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
verify_csrf();
$current_password = (string) ($_POST['current_password'] ?? '');
$new_password = (string) ($_POST['new_password'] ?? '');
$confirm_password = (string) ($_POST['confirm_password'] ?? '');
$stmt = $conn->prepare("SELECT password FROM users WHERE user_id = ?");
$stmt->bind_param('i', $_SESSION['user_id']);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc();
$stmt->close();
if(!$row || !password_verify($current_password, $row['password'])) {
$errors[] = 'รหัสผ่านปัจจุบันไม่ถูกต้อง';
}
if(strlen($new_password) < 8) {
$errors[] = 'รหัสผ่านใหม่ต้องมีความยาวอย่างน้อย 8 ตัวอักษร';
}
if($new_password !== $confirm_password) {
$errors[] = 'รหัสผ่านใหม่และยืนยันรหัสผ่านไม่ตรงกัน';
}
if(empty($errors)) {
$new_hash = password_hash($new_password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("UPDATE users SET password = ? WHERE user_id = ?");
$stmt->bind_param('si', $new_hash, $_SESSION['user_id']);
$stmt->execute();
$stmt->close();
$success = true;
}
}
$back_link = is_student() ? 'student_dashboard.php' : 'index.php';
$page_title = "เปลี่ยนรหัสผ่าน";
$form_width = true;
include("includes/header.php");
?>
<div class="breadcrumb">
<a href="<?php echo $back_link; ?>"><?php echo icon('home', ['class'=>'icon-sm']); ?> หน้าหลัก</a>
<span class="sep">/</span><span class="current">เปลี่ยนรหัสผ่าน</span>
</div>
<div class="page-header"><div><h1>เปลี่ยนรหัสผ่าน</h1></div></div>
<?php if($success): ?>
<div class="alert alert-success" role="status"><?php echo icon('check-circle'); ?><span>เปลี่ยนรหัสผ่านเรียบร้อยแล้ว</span></div>
<?php endif; ?>
<?php if(!empty($errors)): ?>
<div class="alert alert-error" role="alert">
<?php echo icon('alert-triangle'); ?>
<ul>
<?php foreach($errors as $err): ?><li><?php echo htmlspecialchars($err); ?></li><?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<div class="card">
<form method="POST" action="" data-guard-submit>
<?php echo csrf_field(); ?>
<div class="form-group">
<label class="form-label" for="current_password">รหัสผ่านปัจจุบัน<span class="required">*</span></label>
<div class="input-with-action">
<input type="password" id="current_password" name="current_password" class="input" required autocomplete="current-password">
<button type="button" class="input-action-btn" data-toggle-password="current_password" aria-label="แสดงรหัสผ่าน" aria-pressed="false"><?php echo icon('eye'); ?></button>
</div>
</div>
<div class="form-group">
<label class="form-label" for="new_password">รหัสผ่านใหม่<span class="required">*</span></label>
<div class="input-with-action">
<input type="password" id="new_password" name="new_password" class="input" required minlength="8" autocomplete="new-password">
<button type="button" class="input-action-btn" data-toggle-password="new_password" aria-label="แสดงรหัสผ่าน" aria-pressed="false"><?php echo icon('eye'); ?></button>
</div>
<span class="form-help">อย่างน้อย 8 ตัวอักษร</span>
</div>
<div class="form-group">
<label class="form-label" for="confirm_password">ยืนยันรหัสผ่านใหม่<span class="required">*</span></label>
<div class="input-with-action">
<input type="password" id="confirm_password" name="confirm_password" class="input" required minlength="8" autocomplete="new-password">
<button type="button" class="input-action-btn" data-toggle-password="confirm_password" aria-label="แสดงรหัสผ่าน" aria-pressed="false"><?php echo icon('eye'); ?></button>
</div>
</div>
<div class="btn-group mt-4">
<button type="submit" class="btn btn-primary"><?php echo icon('save', ['class'=>'icon-sm']); ?> บันทึกรหัสผ่านใหม่</button>
<a href="<?php echo $back_link; ?>" class="btn btn-secondary">ยกเลิก</a>
</div>
</form>
</div>
<?php include("includes/footer.php"); ?>