At some point in time in your web development career you will be asked to create a login script for an existing application. This is pretty easy to implement with basic PHP, but lets take a look at how to do it with Zend. Also, if you are working on existing project chances are it would not have been done using the Zend MVC structure. So let see how to create this login using the Zend Auth Library as a standalone

Ok first the login page

<?php 
require_once 'Zend/Auth.php'; 
require_once('../config/config.php'); 
require_once '../application/classes/MyAuthAdapter.php'; 
require_once '../application/models/User.php'; 
    
class login 
{ 	 	
	public function CheckLogin()
	{ 		
		$adapter = new MyAuthAdapter('admin', 'admin'); 		
		$result = 	Zend_Auth::getInstance()-->authenticate($adapter);
		if(Zend_Auth::getInstance()->hasIdentity())
		{
			echo 'auth suceeeded foward to relavant page';
		}
		else
		{
			//auth failed
			p( $result->getMessages());
		}
	}

}

echo "trying to log in";
$l = new login();
$l->CheckLogin();

Notice all the require_once statements. Because this is standalone, Zend is not automatically handling the includes for us. This is the file structure for my app, feel free to place your files where ever you wish.

Here is the class that actually attempts the authentication and returns messages as needed

<?php 
require_once 'Zend/Auth/Adapter/Interface.php'; 
require_once '../application/models/User.php'; 

class MyAuthAdapter implements Zend_Auth_Adapter_Interface   
{ 	
	const NOT_FOUND_MSG = "Account not found"; 	
	const BAD_PW_MSG = "Password is invalid"; 	 	 	
	
	protected $user; 	
	protected $password = ""; 	
	protected $username = ""; 	 	
	
	public function __construct($username, $password) 	
	{ 		
		$this->username = $username;
		$this->password = $password;

		$this->user = new User();

	}

	/**
     * Performs an authentication attempt
     *
     * @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
     * @return Zend_Auth_Result
     */
	public function authenticate()
	{
		try
		{
			$this->user->authenticate($this->username, $this->password);
			return $this->createResult(Zend_Auth_Result::SUCCESS);
		}catch (Exception $e){
			if($e->getMessage() == User::WRONG_PW)
				return $this->createResult(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, array(self::BAD_PW_MSG));
			if($e->getMessage() == User::NOT_FOUND)
				return $this->createResult(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, array(self::NOT_FOUND_MSG));
		}
	}

	private function createResult($code, $messages = array())
	{
		return new Zend_Auth_Result($code, $this->user,$messages );
	}
}

and finally, this class gets the code from the db

<?php
class User 
{ 	
	const NOT_FOUND = 1; 	
	const WRONG_PW  = 2; 	 	
	
	private $db; 	 	
	
	function __construct() 	
	{ 		
		$this->db = Zend_Db_Table::getDefaultAdapter();
	}

    public function authenticate($username, $password)
    {
    	$rowset = self::GetUserByUsername($username);
        if($rowset)
        {
        	if($rowset['Password'] == $password)
        	{
        		return $rowset;
        	}
        		throw new Exception(self::WRONG_PW);
        }
       	throw new Exception(self::NOT_FOUND);
    }

	private function GetUserByUsername($username)
	{
		$select = $this->db->select()
				       ->from('users')
				       ->where('Username = ?', $username );
		return $this->db->fetchRow($select);
	}

}

So here you have it. Using Zend Auth without as the MVC framework.

Feel free to comment, ask questions or make it better.