Mini Shell
<?php
// เรียกใช้ไฟล์ session.php ของระบบ (เพื่อใช้ดึงตัวแปร $s_user_id และ $link มาทำงาน)
include_once 'session.php';
// ==========================================
// ส่วนประมวลผลการเปลี่ยนรหัสผ่าน (AJAX)
// ==========================================
if (isset($_POST['action'])) {
header('Content-Type: application/json; charset=utf-8');
$action = $_POST['action'];
if ($action == 'change_password') {
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
$salt = 'tikde78uj4ujuhlaoikiksakei896';
// 1. เช็คว่ากรอกข้อมูลครบหรือไม่
if (empty($old_password) || empty($new_password) || empty($confirm_password)) {
echo json_encode(['status' => 'error', 'message' => 'กรุณากรอกข้อมูลให้ครบทุกช่อง!']);
exit();
}
// 2. เช็คว่ารหัสผ่านใหม่ ตรงกับ ยืนยันรหัสผ่านใหม่ หรือไม่
if ($new_password !== $confirm_password) {
echo json_encode(['status' => 'error', 'message' => 'รหัสผ่านใหม่ และ ยืนยันรหัสผ่านใหม่ ไม่ตรงกัน!']);
exit();
}
// 3. ตรวจสอบรหัสผ่านเดิมว่าถูกต้องหรือไม่ (อ้างอิงจาก $s_user_id ใน session)
$hash_old_password = hash_hmac('sha256', $old_password, $salt);
$check_sql = "SELECT * FROM user WHERE user_id = '$s_user_id' AND password = '$hash_old_password'";
$check_res = mysqli_query($link, $check_sql);
if (mysqli_num_rows($check_res) == 0) {
echo json_encode(['status' => 'error', 'message' => 'รหัสผ่านเดิมไม่ถูกต้อง!']);
exit();
}
// 4. บันทึกรหัสผ่านใหม่ลงฐานข้อมูล
$hash_new_password = hash_hmac('sha256', $new_password, $salt);
$update_sql = "UPDATE user SET password = '$hash_new_password' WHERE user_id = '$s_user_id'";
if (mysqli_query($link, $update_sql)) {
echo json_encode(['status' => 'success', 'message' => 'เปลี่ยนรหัสผ่านเรียบร้อยแล้ว']);
} else {
echo json_encode(['status' => 'error', 'message' => 'เกิดข้อผิดพลาดในการบันทึก: ' . mysqli_error($link)]);
}
exit();
}
}
?>
<!doctype html>
<html lang="en">
<?php include 'head.php'; ?>
<body>
<div class="wrapper">
<?php include 'leftside.php'; ?>
<div class="main-panel">
<?php include 'header.php'; ?>
<div class="content">
<div class="container-fluid">
<div class="col-md-8 col-md-offset-2">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="home.php">หน้าหลัก</a></li>
<li class="breadcrumb-item active" aria-current="page">เปลี่ยนรหัสผ่าน</li>
</ol>
</nav>
<div class="card">
<div class="card-header card-header-icon" data-background-color="rose">
<i class="material-icons"><i class="fas fa-key"></i></i>
</div>
<h4 class="card-title" style="font-weight: bold;">เปลี่ยนรหัสผ่าน (Change Password)</h4>
<div class="card-content">
<form id="formChangePassword" method="POST">
<input type="hidden" name="action" value="change_password">
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">รหัสผ่านเดิม</label>
<input type="password" class="form-control" name="old_password" id="old_password" required>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">รหัสผ่านใหม่</label>
<input type="password" class="form-control" name="new_password" id="new_password" required minlength="4">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">ยืนยันรหัสผ่านใหม่ อีกครั้ง</label>
<input type="password" class="form-control" name="confirm_password" id="confirm_password" required minlength="4">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-success btn-round" id="btnSave" style="font-size: 16px; padding: 12px 30px;">
<i class="material-icons"><i class="fas fa-save"></i></i> ยืนยันการเปลี่ยนรหัสผ่าน
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include 'footer1.php'; ?>
</div>
</div>
<?php include 'footer2.php'; ?>
<script>
$(document).ready(function(){
// Submit ฟอร์มเปลี่ยนรหัสผ่าน
$('#formChangePassword').submit(function(e){
e.preventDefault();
var new_pw = $('#new_password').val();
var confirm_pw = $('#confirm_password').val();
// ตรวจสอบฝั่งหน้าบ้านก่อนส่งไปหลังบ้าน
if(new_pw !== confirm_pw) {
swal("แจ้งเตือน!", "รหัสผ่านใหม่ และ ยืนยันรหัสผ่าน ไม่ตรงกัน!", "warning");
return false;
}
$.ajax({
type: 'POST',
url: window.location.href,
data: $(this).serialize(),
dataType: 'json',
beforeSend: function() {
$('#btnSave').prop('disabled', true).text('กำลังดำเนินการ...');
},
success: function(response){
$('#btnSave').prop('disabled', false).html('<i class="material-icons"><i class="fas fa-save"></i></i> ยืนยันการเปลี่ยนรหัสผ่าน');
if(response.status === 'success'){
swal({
title: "สำเร็จ!",
text: response.message,
type: "success"
}, function(){
// เปลี่ยนรหัสผ่านสำเร็จ ให้เด้งกลับไปหน้าหลัก
window.location.href = "home.php";
});
} else {
swal("ข้อผิดพลาด!", response.message, "error");
}
},
error: function(xhr) {
$('#btnSave').prop('disabled', false).html('<i class="material-icons"><i class="fas fa-save"></i></i> ยืนยันการเปลี่ยนรหัสผ่าน');
swal("ข้อผิดพลาดเซิร์ฟเวอร์!", xhr.responseText.substring(0, 150) + "...", "error");
}
});
});
});
</script>
</body>
</html>
Zerion Mini Shell 1.0