So my goal is to create a login page that redirects to another page. However when I try to login, it out puts "invalid username or password" which tells me that there is something wrong in the php code preceding the else statement. However after spending hours trying to figure it out I couldn't. Unfortunately for me coding is not my strong suit. Any help would be appreciated.
<?php
// ensure page is not cached
require_once "nocache.php";
$errorMessage = '';
// check that the form has been submitted
if (isset($_POST['submit'])) {
// check that username and password were entered
if (empty($_POST['username']) || empty($_POST['pword'])) {
$errorMessage = "Both username and password are required";
} else {
// connect to the database
require_once 'conn.php';
// parse username and password for special characters
$username = $dbConn->escape_string($_POST['username']);
$password = $dbConn->escape_string($_POST['pword']);
// hash the password so it can be compared with the db value
$hashedPassword = hash('sha256', $password);
// query the db
$sql = "SELECT id FROM leagueadmin WHERE email='$username' and password = '$hashedPassword'";
$rs = $dbConn->query($sql);
// check number of rows in record set. What does this mean in this context?
if ($rs->num_rows) {
// start a new session for the user
session_start();
// Store the user details in session variables
$user = $rs->fetch_assoc();
$_SESSION['who'] = $user['id'];
// Redirect the user to the secure page
header('Location: scoreentry.php');
} else {
$errorMessage = "Invalid Username or Password";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Form</title>
<style>
input[type="text"], input[type="password"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/login.css">
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p style="color:red;"><?php echo $errorMessage; ?></p>
<div class="input-box">
<label for="username">Username:</label>
<input type="text" name="username" maxlength="50" id="username">
</div>
<div class="input-box">
<label for="pword">Password:</label>
<input type="password" name="pword" maxlength="100" id="pword">
</div>
<div class="input-box">
<input type="submit" value="Login" name="submit">
</div>
</form>
</body>
</html>