-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
81 lines (62 loc) · 1.88 KB
/
login.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
<?php
session_start();
require_once 'function.php';
if (isset($_COOKIE['key']) && isset($_COOKIE['id'])) {
$id = $_COOKIE['id'];
$key = $_COOKIE['key'];
$result = mysqli_query($koneksi, "SELECT * FROM users WHERE id='$id'");
$row = mysqli_fetch_assoc($result);
if ($key === hash('sha256', $row['username'])) {
$_SESSION['login'] = true;
}
}
if (isset($_SESSION['login'])) {
header('Location: index.php');
}
if (isset($_POST["login"])) {
$username = $_POST["username"];
$password = $_POST["password"];
$result = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username'");
if (mysqli_num_rows($result) === 1) {
$row = mysqli_fetch_assoc($result);
if (password_verify($password, $row['password'])) {
$_SESSION['login'] = true;
if (isset($_POST['remember'])) {
$key = hash('sha256', $row['username']);
setcookie('key', $key, time() + 60);
setcookie('id', $row['id'], time() + 60);
}
echo "<script>
alert('anda berhasil login');
document.location.href='index.php';
</script>";
}
}
$error = true;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<h1>HALAMAN LOGIN</h1>
<?php if (isset($error)) : ?>
<h4 style="color: red;">Username / Password Salah</h4>
<?php endif; ?>
<br>
<form method="post" action="">
<label for="username">Username</label>
<input type="text" name="username" id="username"><br>
<label for="password">Password</label>
<input type="password" name="password" id="password"><br>
<label for="remember">Remember</label>
<input type="checkbox" name="remember" id="remember">
<button type="submit" name="login">Login</button>
</form>
<a href="register.php">REGISTER</a>
</body>
</html>