food-inventory/login.php
2019-11-21 23:27:02 +01:00

101 lines
3.8 KiB
PHP

<?php
// Include util functions
require_once("./assets/php/utils.php");
// Check if the user is already logged in, if yes then redirect him to welcome page
if (is_connected()) {
header("location: welcome.php");
exit;
}
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";
// Processing form data when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if username is empty
if (empty(trim($_POST["username"]))) {
$username_err = "Please enter your username.";
} else {
$username = trim($_POST["username"]);
}
// 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($username_err) && empty($password_err)) {
// Check if username exists, if yes then verify password
if (get_username_count($username) == 1) {
if (correct_email_password($username, $password)) {
if (is_https()) {
connect_user(get_user_id_from_email($username), isset($_POST["stay_loggedin"]));
// Redirect user to welcome page
header("location: welcome.php");
} else {
$username_err = "Please use HTTPS.";
}
} else {
// Display an error message if password is not valid
$username_err = "Invalid Username/Password.";
}
} else {
$username_err = "Invalid Username/Password.";
}
}
}
?>
<!DOCTYPE html>
<?php include_once("./assets/php/copyright.php"); ?>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- TODO : ADD SEO STUFF -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="/assets/css/reset.css" rel="stylesheet">
<link href="/assets/css/common.css" rel="stylesheet">
<link href="/assets/css/login.css" rel="stylesheet">
<title>Food inventory - Login</title>
</head>
<body>
<div class="center30left white">
<h2 class="montserrat" id="title">Welcome back</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="flex-container flex-evenly">
<input type="email" name="username" class="form-control halo-hover <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>" value="<?php echo $username; ?>" placeholder="Username">
<input type="password" name="password" class="form-control halo-hover <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>" placeholder="Password">
</div>
<div class="flex-container flex-evenly">
<div class="error">
<?php
if (!empty($username_err)) echo $username_err; else echo $password_err;
?>
</div>
</div>
<div class="remember-me flex-container">
<label class="checkbox-container">Remember me
<input type="checkbox" name="stay_loggedin" />
<span class="checkmark"></span>
</label>
</div>
<div class="form-group flex-container flex-vertical-center flex-evenly">
<input type="submit" class="halo-hover login-button" value="Login">
<a href="register.php" class="login-button halo-hover other-buttons">You don't have an account? Register!</a>
<a href="/" class="login-button halo-hover other-buttons">Main page</a>
</div>
</form>
</div>
</body>
</html>