// verify username and password aren't blank
if (count($errors) == 0) {
// setup the authentication adapter
$adapter = new Zend_Auth_Adapter_DbTable($this->db,
'users',
'username',
'password',
'md5(?)');
$adapter->setIdentity($username);
$adapter->setCredential($password);
// try and authenticate the user
$result = $auth->authenticate($adapter);
var_dump($result);
if ($result->isValid()) {
$user = new DatabaseObject_User($this->db);
$user->load($adapter->getResultRowObject()->user_id);
// record login attempt
$user->loginSuccess();
// create identity data and write it to session
$identity = $user->createAuthIdentity();
$auth->getStorage()->write($identity);
// send user to page they originally request
$this->_redirect($redirect);
}
// record failed login attempt
DatabaseObject_User::LoginFailure($username,
$result->getCode());
$errors['username'] = 'Your login details were invalid';
}
}
///////////////HTML(INDEX.PHP)/////////////
<form id="form" method="post" action="./login.php">
<label>Username: </label>
<br />
<input type="text" name="username" value="" />
<br />
<br />
<label>Password: </label>
<br />
<input type="password" name="password" value="" />
<br />
<br />
<input type="submit" name="submit" value="Login" />
</form>
//////////////////LOGIN.PHP///////////////////////
<?php include("authenticate.php"); //We need to include the file the authentication class is in for later use
//The same way as normal, check to see if the form was submitted if(isset($_POST["submit"])) {
//Validate the inputs and clean them
//Because validating the data is really outside the scope of this, I'll just do it in pseudo-code
/* PSEUDO-CODE */
if username field has a value
check the value is a valid format for a username (IE doesn't contain characters you don't allow)
if the value is valid
clean the value just in case
end if
end if
if password field has a value
check the value is a valid format for a password(IE doesn't contain characters you don't allow)
if the value is valid
clean the value just in case
hash the password
end if
end if
/* END PSEUDO-CODE */
//At this point, assume you have two cleaned and validated variables for the username and password
//$user and $pass
//If the data passed turns out to be valid, lets start authenticating
$auth = new Authentication();
//Create a new instance of the Authentication class
//If the method "Login" in the authentication object returns "true"
if($auth->Login($user, $pass))
{
//Do whatever you need to when the user is logged in
}
//If the method returns false
else {
//Ouput your error message
}
}
?>
////////////////////////////////AUTH.PHP//////////////////////
<?php
//This class will only work with PHP 5 and above due to the magic method __construct and the way global variables are created
class Authentication
{ public $userData = array();
private $dbLink;
//This method is called when we create a new instance of the class. We don't need to supply any extra information to create the object, so we don't give it any arguments
function __construct()
{
//Initialising variables is a good idea on a constructor $this->userDate = array();
//Connect to a database in the normal way and assign the link to $this->dbLink
}
//This is the method that is called in login.php to execute the login of the user
public function Login($username, $password)
{
//You might want to validate the data here too, but you don't HAVE to because you already validated it in login.php
//Using $this->dbLink, perform a standard database query to see if there is a record in there with the same username and password as the ones supplied
//If there is
return true;
//If there isn't
return false;
//You'd also want to put all the user data in to $this->userData and set up sessions and all that good stuff, but I'm too lazy to go through all that. I'm sure you can do that bit for yourself }
}
?>
object(Zend_Auth_Result)#5