-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
105 lines (93 loc) · 3.04 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
<?php
session_start();
include 'db.php';
// Check if the user is already logged in and prevent redirect loop
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
header("Location: dashboard.php");
exit();
}
//inlcude db.php
// Initialize error message
$error_message = "";
// Check if request method is POST to handle the form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$admin_email = $_POST['useremail'];
$admin_password = $_POST['userpassword'];
// Use prepared statement to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM userdata WHERE email = ? AND password = ?");
$stmt->bind_param("ss", $admin_email, $admin_password);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Fetch the username and set session variables
$row = $result->fetch_assoc();
$username = $row['username'];
// Set session variables
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
// Redirect to the dashboard
header("Location: dashboard.php");
exit();
} else {
// Set error message if login fails (no output before header)
$error_message = "Invalid email or password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<title>Home Page</title>
</head>
<body>
<section
class="vh-100 d-flex justify-content-center align-items-center bg-light">
<form
class="container py-3 px-4 rounded-4 h-auto w-25 mx-auto"
style="background-color: #eeeeee"
action="index.php"
method="post">
<h2 class="text-center">Login</h2>
<!-- Email input -->
<div data-mdb-input-init class="form-outline mb-4">
<label class="form-label" for="useremail">Email address</label>
<input
type="email"
id="useremail"
name="useremail"
class="form-control" />
</div>
<!-- Password input -->
<div data-mdb-input-init class="form-outline mb-4">
<label class="form-label" for="userpassword">Password</label>
<input
type="password"
id="userpassword"
class="form-control"
name="userpassword" />
</div>
<!-- Submit button -->
<div class="d-flex justify-content-center align-items-center">
<button
type="submit"
data-mdb-button-init
data-mdb-ripple-init
class="btn btn-dark btn-block mb-2 d-flex justify-content-center">
Sign in
</button>
</div>
</form>
</section>
</body>
</html>