I am really struggling to get some login stuff working. I figured out how to put in some error handlers and exception handlers. Now when my form passes the values to the member.php5 page for processing I get the following exception message.
Caught exception:
Login function failed - exception caught at line 63: Object id #2
What would be Object Id #2 and what do you think I am doing to cause the problem?
Here is the member.php5 page:
<?php
session_start();
// include function files for this application
require_once('includes/error_fns.php5');
//set handlers for debugging:
set_error_handler("on_error");
set_exception_handler("handle_exception");
//create short variable names
$username = (isset($_POST['username'])) ? $_POST['username'] : NULL;
$passwd = (isset($_POST['passwd'])) ? $_POST['passwd'] : NULL;
function db_connect()
{
$result = new mysqli("localhost", "omgma_omgmauser", "XYZ213", "omgma_members");
if (!$result)
throw new Exception('db_connect Could not connect to database server');
else
return $result;
}
function login($username, $passwd)
// check username and password with db
// if yes, return true
// else throw exception
{
// connect to db
$conn = db_connect();
// check if username is unique
$result = $conn->query('select * from user
where username="$username"
and password =("$passwd")');
if (!$result)
throw new Exception('Login function could not log you in: line 18');
if ($result->num_rows>0)
return true;
else
throw new Exception('Login function could not log you in: line 23.');
}
//This is where the login logic starts
if ($username && $passwd)
// they have just tried logging in - echo out so I know what is happening
echo "<p>The username is $username and password is $passwd</p>";
{
try
{
login($username, $passwd);
// if they are in the database register the user id
$_SESSION['valid_user'] = $username;
echo "logged in as $username";
}
catch(Exception $e)
{
// unsuccessful login
throw new Exception("<p>Login function failed - exception caught at line 63:\n".$e ."</p>");
}
}
?>