-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
172 lines (144 loc) · 6.17 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
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
<?php
require_once 'include/config.php';
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = $error_msg = "";
session_start();
//session_unset();
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if username is empty
if(empty(trim($_POST["username"]))){
$username_err = 'Mohon masukkan username.';
} else{
$username = trim($_POST["username"]);
}
// Check if password is empty
if(empty(trim($_POST['password']))){
$password_err = 'Mohon masukkan password.';
} else{
$password = trim($_POST['password']);
}
// Validate credentials
if(empty($username_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT username, nama, password, done, name_team FROM tb_user JOIN tb_team ON team=id_team WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = $username;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $username, $nama, $hashed_password, $done, $team);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
if($done==0){
/* Password is correct, so start a new session and
save the username to the session */
//session_start();
$_SESSION['username'] = $username;
$_SESSION['nama'] = $nama;
$_SESSION['done'] = $done;
$_SESSION['team'] = $team;
header("location: index.php");
} else{
// Display an error message if password is not valid
$error_msg = 'Anda telah menyelesaikan tes.';
}
} else{
// Display an error message if password is not valid
$password_err = 'Password tidak sesuai.';
}
}
} else{
// Display an error message if username doesn't exist
$username_err = 'Username tidak terdaftar.';
}
} else{
echo "Terjadi kesalahan. Mohon coba kembali.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login Quiz Application</title>
<link rel="icon" href="" type="image/png" />
<meta name="description" content="Quiz Application">
<meta name="keywords" content="HTML,PHP,MySQL,JavaScript,Quiz,Web,Application">
<meta name="author" content="Made Indra Wira Pramana">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="css/bootstrap-reboot.min.css" type="text/css">
<link rel="stylesheet" href="css/bootstrap-grid.min.css" type="text/css">
<link rel="stylesheet" href="css/font-awesome.min.css" type="text/css">
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
/*phone layout and less*/
@media (max-width: 575.98px) {
.container{
width: 80%;
}
}
/*tablet layout and up*/
@media (min-width: 768px) {
.container{
width: 30%;
}
}
</style>
</head>
<body style="font-family: 'Roboto', sans-serif;">
<div class="container" style="padding: 10% 0%;min-width: 30%; max-width: 80%; margin: 0 auto; ">
<div class="card">
<div class="card-header bg-primary text-white" align="center"><strong><h4>Masuk</h4></strong></div>
<div class="card-body">
<form id="login" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<?php if(!empty($error_msg)){
echo "<center><div class='alert alert-danger' role='alert'><STRONG>Peringatan! </STRONG>$error_msg</div></center>";;
}?>
<div class="form-group">
<div class="input-group mb-3"><div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user"></i></span>
</div>
<input type="text" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username; ?>" name = "username" placeholder="Username" style="" />
</div>
<span class="help-block" style="color: red;"><?php echo $username_err; ?></span>
</div>
<div class="form-group">
<div class="input-group mb-3"><div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock"></i></span>
</div>
<input type="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>" name = "password" placeholder="Password" />
</div>
<span class="help-block" style="color: red;"><?php echo $password_err; ?></span>
</div>
<div class="form-group text-center">
<input type="submit" class="btn btn-outline-primary" value="Login">
<!-- <a href="#" class="btn btn-link">Lupa Password</a> -->
</div>
<center>Silahkan <a href="register.php">klik disini</a> jika belum terdaftar.</center>
</form>
</div>
<div class="card-footer bg-primary text-white" align="center">© Made Indra Wira Pramana </div>
</div>
</div>
</body>
</html>