I'm working on a PHP web application for a small business, and I am now trying to incorporate a login system to the app. I have my login.php page which is collecting the UN/PW and storing it via $_POST, and I have my UN/PW stored in a MySQL table with a MD5 hash. When I click the login button from login.php, it calls includes/login.inc.php and processes the login. However, I only see a blank page when login.inc.php is called. I'm not sure where my issue is.
login.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="css/login.css" />
</head>
<body>
<form id="login-form" method="post" action="includes/login.inc.php">
<fieldset>
<legend>Login to Inventory System</legend>
<p>Please enter your username and password to access the Inventory system</p>
<label for="username">
<input type="text" name="username" id="username" />Username:
</label>
<label for="password">
<input type="password" name="password" id="password" />Password:
</label>
<label>
<input type="submit" name="submit" id="submit" value="Login" />
</label>
</fieldset>
</form>
</body>
</html>
login.inc.php:
<?php
// Include required MySQL configuration file and functions
require_once('config.inc.php');
require_once('functions.inc.php');
// Start session
session_start();
// Check if user is already logged in
if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
} else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR
(!ctype_alnum($_POST['username'])) ) {
redirect('../login.php');
}
// Connect to database
$mysqli = @new mysqli('localhost', 'username', 'password', 'db_name');
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s", mysqli_connect_error());
exit();
}
// Escape any unsafe characters before querying database
$username = $mysqli->real_escape_string($_POST['username']);
$password = $mysqli->real_escape_string($_POST['password']);
// Construct SQL statement for query & execute
$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'";
$result = $mysqli->query($sql);
// If one row is returned, username and password are valid
if (is_object)($result) && $result->num_rows == 1) {
// Set session variable for login status to true
$_SESSION['logged_in'] = true;
redirect('../index.php');
} else {
// If number of rows returned is not one, redirect back to login screen
redirect('../login.php');
}
}
?>
Does anyone see my problem?
Thanks!