I have created a test login page that, as most do, takes in two values from the user (username and password) and the processes it.
For this I have opted to use jQuery and AJAX to pass the results to a separate page to be processed, checked and validated before allowing access.
My main front end uses Twitter Bootstrap v3.3.5 and jQuery v1.11.3 (I know there are newer but it's what I am used to until I learn the newer one).
The front end looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- Custom styles and scripts -->
<link rel="stylesheet" href="style/gw_login.css" />
<script src="script/jquery.ajax.login.js"></script>
<!-- //Custom styles and scripts -->
</head>
<body class="container-fluid">
<div class="row">
<div class="container">
<div class="row">
<h1>Login</h1>
<table class="table table-condesned table-striped">
<tr>
<td><p><label for="username">Username</label></p></td>
<td><input type="text" id="username" placeholder="Username" class="form-control" /></td>
</tr>
<tr>
<td><p><label for="password">Password</label></p></td>
<td><input type="password" id="password" placeholder="password" class="form-control" /></td>
</tr>
<tr>
<td colspan="2"><button class="btn btn-warning form-control" id="login">Login</button></td>
</tr>
</table>
</div>
<div class="row" id="error"></div>
<!-- //table -->
</div>
<!-- //container -->
</div>
<!-- //row -->
</body>
<!-- //Body -->
</html>
The jQuery code is as such:
$( document ).ready( function ()
{
$( "#login" ).on( "click", function ()
{
$.ajax( {
url: "script/ajax/login.php",
type: "GET",
success: function ( $res )
{
if ( $res != "t" )
{
if ( $res == "f" )
{
$( "#error" ).html( "Username and/or password is incorrect." );
}
else
{
$( "#error" ).html( $res );
return;
}
}
else
{
alert( "Login success." );
}
}
} );
} );
} );
And finally my PHP login script:
<?php
$errors = [];
// Check that username and password exist
if (!isset($_REQUEST['username']) || empty($_REQUEST['username']))
$errors[] = "Please supply a username.";
if (!isset($_REQUEST['password']) || empty($_REQUEST['password']))
$errors[] = "Please supply a password.";
//Check if there have been errors
if (count($errors) > 0)
{
foreach ($errors as $error)
{
print ($error . "<br />");
}
die();
}
// Create database link
$link = new mysqli("server_ip", "username", "password", "database_name");
// Set variables for easier use
$username_safe = $link->real_escape_string($_REQUEST['username']);
$password = ($_REQUEST['password']);
// Allow the user to use both email or chosen username
$query = "SELECT `password` FROM `users` WHERE `username` = '{$username_safe}' or `email` = '{$username_safe}';";
// Perform the query
$query = $link->query($query);
// Check rows are there
if ($query->num_rows > 0)
{
// Fetch the result
while ($res = mysqli_fetch_assoc($query))
{
// Check password against value held in database
print (password_verify($password, $res['password']) ? "t" : "f");
die();
}
}
?>
The expected behaviour changes depending on what it is I am doing, sometimes I want to see Login success when I user values that exist in the database and other times a single error on either the username/password or both.
The behaviours that I expect occur. I wanted to know if there is a better way of doing what I currently am?