TIF_NGANJUK_E41212020/admin/profile/update_password.php

75 lines
2.1 KiB
PHP

<?php include '../template/template1.php'; ?>
<?php
// Ambil data admin yang sedang login
$admin = $_SESSION['user_global_admin'];
$admin_id = $admin->id_admin;
// Tangkap data dari form
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
// Validasi input tidak boleh kosong
if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
echo "<script>
alert('Semua field harus diisi!');
window.location.href = 'index.php';
</script>";
exit;
}
// Ambil password dari database
$query = mysqli_query($conn, "SELECT password FROM admin WHERE id_admin = '$admin_id'");
$data = mysqli_fetch_assoc($query);
$hashed_password = $data['password'];
// Cek apakah password lama cocok
if (!password_verify($current_password, $hashed_password)) {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Password lama salah!',
}).then(() => window.location.href = 'index.php');
</script>";
exit;
}
// Cek apakah password baru dan konfirmasi password sama
if ($new_password !== $confirm_password) {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Konfirmasi password tidak cocok!',
}).then(() => window.location.href = 'index.php');
</script>";
exit;
}
// Hash password baru
$new_hashed_password = password_hash($new_password, PASSWORD_BCRYPT);
// Update password di database
$update = mysqli_query($conn, "UPDATE admin SET password = '$new_hashed_password' WHERE id_admin = '$admin_id'");
if ($update) {
echo "<script>
Swal.fire({
icon: 'success',
title: 'Berhasil!',
text: 'Password berhasil diubah!',
}).then(() => window.location.href = 'index.php');
</script>";
} else {
echo "<script>
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Terjadi kesalahan, coba lagi!',
}).then(() => window.location.href = 'index.php');
</script>";
}
?>