<?php
/*
Available functions:
function __construct($user, $pass, $passcheck = 'md5', $session = FALSE)
function set_db_connection($serveraddr, $serveruser, $serverpass, $serverdbname)
function set_table($dbtable)
function set_fields($userfield, $passfield)
function validate()
function print_query()
function get_user_info()
*/
error_reporting(E_ALL);
include('userchecker.class.php');
//Special super-strong hash encryption function
function stronghash($plain)
{
$hash = sha1(crypt(md5($plain), sha1($plain)));
return $hash;
}
//Get user data
$username = $_POST['username'];
$password = $_POST['password'];
//Set MySQL information
$serveraddr = '127.0.0.1';
$serveruser = 'root';
$serverpass = 'toor';
$serverdbname = 'users';
//Pass username, password and DB information
//Tell the script that the password is hashed using a home-made function
$user = new User($username, $password, 'stronghash');
$user->set_db_connection($serveraddr, $serveruser, $serverpass, $serverdbname);
$user->set_table("users");
$user->set_fields('username', 'passwordhash');
//Start output
?>
<html>
<head>
<title>Simple login page</title>
</head>
<body>
<?php
//If the user was in fact created (ie. there was a username and password) then $user will not be false
if ($user && $user->validate() > 0)
{
//A single user is found
if ($user->validate() == 1)
{
echo "Congratulations, your credentials have been accepted!";
//User is accepted, lets find some info about her and print it
$information = $user->get_user_info();
echo "Here is your info:";
echo nl2br(print_r($information, True));
}
//Multiple users, something is wrong (Mark that calling validate() will not connect to the database again)
else if ($user->validate() == 2)
{
die("Are you trying to hack us?");
}
}
//If user was not found or user has not submitted the form yet
else if ($user->validate() == 0 || !$user)
{
?><form action="" method="post"><input type="text" name="username" /><input type="password" name="password" /><input type="submit" value="Login" /></form><?php
}
//An error occured, print query and die
else
{
die("An error occured using query: \"".$user->print_query()."\", see error log for details");
}
?>
</body>
</html>
|