-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathganti-kata-sandi.php
195 lines (162 loc) · 5.93 KB
/
ganti-kata-sandi.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
session_start();
include 'connect-db.php';
include 'functions/functions.php';
// sesuaikan dengan jenis login
if((isset($_SESSION["login-admin"]) && isset($_SESSION["admin"]))){
$login = "Admin";
$idAdmin = $_SESSION["admin"];
}else if( (isset($_SESSION["login-agen"]) && isset($_SESSION["agen"]))){
$idAgen = $_SESSION["agen"];
$login = "Agen";
}else if ((isset($_SESSION["login-pelanggan"]) && isset($_SESSION["pelanggan"]))){
$idPelanggan = $_SESSION["pelanggan"];
$login = "Pelanggan";
}else {
echo "
<script>
document.location.href = 'index.php';
</script>
";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include "headtags.html"; ?>
<title>Ganti Kata Sandi</title>
</head>
<body>
<?php include 'header.php'; ?>
<h3 class="header col s24 light center">Ganti Kata Sandi</h3>
<form action="" method="POST" class="col s18 center">
<div class="input-field inline">
<input type="password" name="passwordLama" placeholder="Password Lama">
<input type="password" name="password" placeholder="Password Baru">
<input type="password" name="repassword" placeholder="Konfirmasi Password Baru">
<button class="waves-effect blue darken-2 btn" type="submit" name="gantiPassword">Ganti Password</button>
</div>
</form>
<br>
<?php include "footer.php"; ?>
</body>
</html>
<?php
// ubah sandi
if (isset($_POST["gantiPassword"])){
$passwordLama = htmlspecialchars($_POST["passwordLama"]);
$password = htmlspecialchars($_POST["password"]);
$repassword = htmlspecialchars($_POST["repassword"]);
if ($login == 'Admin'){
$idAdmin = $_SESSION["admin"];
$data = mysqli_query($connect, "SELECT * FROM admin WHERE id_admin = $idAdmin");
$data = mysqli_fetch_assoc($data);
if ($passwordLama != $data["password"]) {
echo "
<script>
Swal.fire('Password Lama Salah','','error').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
exit;
}
if ($password != $repassword) {
echo "
<script>
Swal.fire('Password Baru Tidak Sama','','error').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
exit;
}
$query = mysqli_query($connect, "UPDATE admin SET password = '$password' WHERE id_admin = $idAdmin");
if (mysqli_affected_rows($connect) > 0) {
echo "
<script>
Swal.fire('Password Berhasil Diganti','','success').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
}
}else if ($login == "Agen"){
$idAgen = $_SESSION["agen"];
$data = mysqli_query($connect, "SELECT * FROM agen WHERE id_agen = $idAgen");
$data = mysqli_fetch_assoc($data);
if (password_verify($passwordLama, $data["password"])) {
echo "
<script>
Swal.fire('Password Lama Salah','','error').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
exit;
}
if ($password != $repassword) {
echo "
<script>
Swal.fire('Password Baru Tidak Sama','','error').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
exit;
}
//hash
$password = password_hash($password, PASSWORD_DEFAULT);
$query = mysqli_query($connect, "UPDATE agen SET password = '$password' WHERE id_agen = $idAgen");
if (mysqli_affected_rows($connect) > 0) {
echo "
<script>
Swal.fire('Password Berhasil Diganti','','success').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
}
}else if ($login = "Pelanggan"){
$idPelanggan = $_SESSION["pelanggan"];
$data = mysqli_query($connect, "SELECT * FROM pelanggan WHERE id_pelanggan = $idPelanggan");
$data = mysqli_fetch_assoc($data);
// hash
$passwordLama = password_hash($passwordLama, PASSWORD_DEFAULT);
if (password_verify($passwordLama, $data["password"])) {
echo "
<script>
Swal.fire('Password Lama Salah','','error').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
exit;
}
if ($password != $repassword) {
echo "
<script>
Swal.fire('Password Baru Tidak Sama','','error').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
exit;
}
// hash
$password = password_hash($password, PASSWORD_DEFAULT);
$query = mysqli_query($connect, "UPDATE pelanggan SET password = '$password' WHERE id_pelanggan = $idPelanggan");
if (mysqli_affected_rows($connect) > 0) {
echo "
<script>
Swal.fire('Password Berhasil Diganti','','success').then(function() {
window.location = 'ganti-kata-sandi.php';
});
</script>
";
}
}
}
?>