-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
127 lines (100 loc) · 4.14 KB
/
index.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
<?php
session_start();
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: ./admin/index.php");
exit;
}
require_once "./assets/php/config.php";
$username = $password = "";
$usernameError = $passwordError = $loginError = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["Username"]))){
$usernameError = "Please enter your username.";
} else{
$username = trim($_POST["Username"]);
}
if(empty(trim($_POST["Password"]))){
$passwordError = "Please enter your password.";
} else{
$password = trim($_POST["Password"]);
}
if(empty($usernameError) && empty($passwordError)){
$sql = "SELECT UserID, Username, Password FROM users WHERE Username = :username";
if($stmt = $pdo -> prepare($sql)){
$stmt -> bindParam(":username", $param_username, PDO::PARAM_STR);
$param_username = trim($_POST["Username"]);
if($stmt -> execute()){
if($stmt -> rowCount() == 1){
if($row = $stmt -> fetch()){
$id = $row["UserID"];
$username = $row["Username"];
$password = $row["Password"];
session_start();
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
header("location: ./admin/index.php");
}
} else{
$loginError = "Invalid Username or Password.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
unset($stmt);
}
}
unset($pdo);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="./css/style.min.css">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 d-flex flex-column py-4 px-5">
<?php
if(!empty($loginError)){
echo '<div class="alert alert-danger mx-5">' . $loginError . '</div>';
}
?>
<div class="py-3 ps-5">
<h3 class="h-25">Brown Pearl</h3>
</div>
<div class="py-3 px-5">
<h1 class="mb-4 font-weight-bold h3">Log In</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<div class="form-floating mb-2">
<input type="text" name="Username" id="Username" class="form-control <?php echo (!empty($usernameErroror)) ? 'is-invalid' : '';?>" placeholder="Username" value="<?php echo $username;?>">
<label for="Username">Username</label>
<span class="invalid-feedback">
<?php echo $usernameError;?>
</span>
</div>
<div class="form-floating mb-4">
<input type="password" name="Password" id="Password" class="form-control <?php echo (!empty($passwordError)) ? 'is-invalid' : '';?>" placeholder="Password">
<label for="Password">Password</label>
<span class="invalid-feedback">
<?php echo $passwordError;?>
</span>
</div>
<div class="d-grid gap2">
<input type="submit" value="Login" class="btn btn-warning">
</div>
</form>
</div>
</div>
<div class="col-sm-6 px-0 d-none d-sm-block">
<img src="./images/login.jpg" alt="" class="w-100 vh-100">
</div>
</div>
</div>
</body>
</html>