-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
149 lines (130 loc) · 5.67 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
<?php
// Include config file
require_once 'config.php';
// Initialize the session
session_start();
// If session variable is not set it will redirect to login page
if (isset($_SESSION['email']) || !empty($_SESSION['email'])) {
header("location: /");
exit;
}
// Define variables and initialize with empty values
$email = $password = "";
$email_err = $password_err = "";
// Processing form data when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if email is empty
if (empty(trim($_POST["email"]))) {
$email_err = 'Please enter email.';
} else {
$email = trim($_POST["email"]);
}
// Check if password is empty
if (empty(trim($_POST['password']))) {
$password_err = 'Please enter your password.';
} else {
$password = trim($_POST['password']);
}
// Validate credentials
if (empty($email_err) && empty($password_err)) {
// Prepare a select statement
$sql = "SELECT user_id, first_name, middle_name, last_name, email, password, role FROM app_user WHERE email = ?";
if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_email);
// Set parameters
$param_email = $email;
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
// Store result
mysqli_stmt_store_result($stmt);
// Check if email exists, if yes then verify password
if (mysqli_stmt_num_rows($stmt) == 1) {
// Bind result variables
mysqli_stmt_bind_result($stmt, $user_id, $first_name, $middle_name, $last_name, $email, $hashed_password, $role);
if (mysqli_stmt_fetch($stmt)) {
if ($password === $hashed_password) {
/* Password is correct, so start a new session and
save the email to the session */
session_start();
$_SESSION['email'] = $email;
$_SESSION['user_id'] = $user_id;
$_SESSION['first_name'] = $first_name;
$_SESSION['middle_name'] = $middle_name;
$_SESSION['last_name'] = $last_name;
if ($role == 'admin') {
$_SESSION['role'] = $role;
}
header("location: /");
} else {
// Display an error message if password is not valid
$password_err = 'The password you entered was not valid.';
}
}
} else {
// Display an error message if email doesn't exist
$email_err = 'No account found with that email.';
}
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OMTS</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 text-center">
<br><br><br><br>
<h1>Welcome!</h1>
<br>
<img src="/img/logo.png" alt="logo" class="responsive">
<h2>Online Movie Ticket Service</h2>
<p>Your One Stop Shop!</p>
<br><br><br><br>
</div>
<br><br><br><br>
<br><br><br><br>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h2>Login</h2>
<p>Please fill in your credentials to login.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
<label>Email</label>
<input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
<span class="help-block"><?php echo $email_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" name="password" class="form-control">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Login">
</div>
<p>Don't have an account? <a href="/register">Sign up now</a>.</p>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>