97 lines
3.0 KiB
PHP
97 lines
3.0 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), false);
|
|
|
|
// 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>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Login</title>
|
|
<style type="text/css">
|
|
body {
|
|
font: 14px sans-serif;
|
|
}
|
|
|
|
.wrapper {
|
|
width: 350px;
|
|
padding: 20px;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="wrapper">
|
|
<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($username_err)) ? 'has-error' : ''; ?>">
|
|
<label>Username</label>
|
|
<input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
|
|
<span class="help-block"><?php echo $username_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.php">Sign up now</a>.</p>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
|
|
</html>
|