<?php include_once('inc/connection.php');?>
<?php
session_start(); ?>
<?php
// check for form submission
if (isset($_POST['submit'])) {
$errors = array();
// check if the username and password has been entered
if (!isset($_POST['email']) || strlen(trim($_POST['email'])) < 1 ) {
$errors[] = 'Username is Missing / Invalid';
}
if (!isset($_POST['password']) || strlen(trim($_POST['password'])) < 1 ) {
$errors[] = 'Password is Missing / Invalid';
}
// check if there are any errors in the form
if (empty($errors)) {
// save username and password into variables
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
// prepare database query
$query = "SELECT * FROM user
WHERE email = '{$email}'
AND password = '{$password}'
LIMIT 1";
$result_set = mysqli_query($connection, $query);
if ($result_set) {
// query succesfful
if (mysqli_num_rows($result_set) == 1) {
// valid user found
$user=mysqli_fetch_assoc($result_set);
$_SESSION['user_id'] = $user['id'];
$_SESSION['first_name'] = $user['first_name'];
//last login
$query= "UPDATE user SET last_login = NOW()";
$query .= "WHERE id = {$_SESSION['user_id']} LIMIT 1";
$result_set = mysqli_query($connection, $query);
if(!$result_set){
DIE ("DATA BASE FAIL");
}
// redirect to index.php
header('Location: users.php');
} else {
// user name and password invalid
$errors[] = 'Invalid email / Password';
}
} else {
$errors[] = 'database query failed';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Log In - User Management System</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="login">
<form action="login.php" method="post">
<fieldset>
<legend><h1>Log In</h1></legend>
<?php
if (isset($errors) && !empty($errors)) {
echo '<p class="">Invalid Username / Password</p>';
}
?>
<?php
if (isset($_GET['logout'])) {
echo '<p class="info">You have successfully logged out from the system</p>';
}
?>
<p>
<label for="">Username:</label>
<input type="text" name="email" id="" placeholder="Email Address">
</p>
<p>
<label for="">Password:</label>
<input type="password" name="password" id="" placeholder="Password">
</p>
<p>
<button type="submit" name="submit">Log In</button>
</p>
</fieldset>
</form>
</div> <!-- .login -->
</body>
</html>
<?php mysqli_close($connection); ?>
Comments
Post a Comment