Hey Everyone-
I cant really determine the issue I am having. I have a login.php page that requires a username and password. Once the user hits the submit button the form is redirected to a functions.php page which is supposed to query the database and validate the user and password or otherwise return error messages. Whats happening is when the user hits submit the url is directed to the functions page and it is blank.
So I cant determine whether it is not connecting to the database or if it is connecting and my syntax is incorrect somewhere. Although, I dont get any syntax errors.
Upon successful login I would like the user to be redirected to an index.php page but am not sure where to insert that.
Here is the code for my functions.php page and my login.php page. Thanks so much in advance for taking a quick look. Any thoughts are greatly appreciated.
login.php-
<?php
include ('includes/constants.php');
//establishing a connection
$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$connection) {
die ("Could not connect to database" . mysql_error());
}
//selecting the database to use
$db_select = mysql_select_db (DB_NAME, $connection);
if (!$db_select) {
die ("Databse selection has failed:" . mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<title>Hockey Home Page</title>
<link rel="stylesheet" media="all" type="text/css" href="styles/styles.css" />
</head>
<body bgcolor="#FFFFFF">
<div id="header">
<h1>Welcome to my site about Hockey</h1>
</div>
<div id="leftbar">
</div>
<div id="main">
<form action="includes/functions.php" method="post">
<h1> Please Log In</h1>
<p>User Name:<br/>
<input type="text" name="user_name" size= "20" maxlength="20" /></p><br/>
<p>Password:<br/>
<input type="password" name="user_pass" size= "20" maxlength="12" /></p><br/>
<p><input type="submit" name="submit" value="Login" /></p>
</form>
</div>
<div id="footer">Site Design by MShane | © 2012 | All Rights Reserved</div>
</body>
</html>
<?php
//close the connection to the database
if (isset($connection)) {
mysql_close($connection);
}
?>
here is the functions.php page which is called-
<?php
function check_login ($connection, $user_name='', $user_pass='') {
$global = $connection;
$errors = array(); //Initialize the error array
//validate the e-mail address
if (empty($user_name)) {
$errors[] = 'Oops, you forgot to enter a user name.';
} else {
$n = mysqli_real_escape_string($connection, trim($user_name));
}
//validate the password
if (empty($user_pass)) {
$errors[] = 'Opps, you forgot to enter a password.';
} else {
$p = mysqli_real_escape_string($connection, trim($pass));
}
if (empty($errors)) {
//if everything checks out ok
//retrieve the user_id and first_name for that username and password combination
$q = "SELECT users_id, user_name
FROM users
WHERE user_name = '$n'
AND user_pass = '$p'";
$r = @mysqli_query($connection, $q); //run the query
//check the result:
if (mysqli_num_rows($r) == 1) {
//fetch the record
$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
//return true and the record
return array(true, $row);
} else {
//not a match!!
$errors[] = 'Sorry, the user name and/or password you provided do not match those on file.';
}
} // end of empty errors IF
//return false and the errors
return array(false, $errors);
} //end of check login function
?>