Login simple with Zend_Auth

Almost every Web application today need a way to track information about their users, actions and behaviors. Best way to accomplish this is to authenticate user so you can keep more detailed information about what user do and what are users habits. There are lot of ways to implement login authentication, but in this post we will focus on how to do implement this using Zend_Auth  library.

In following example we will assume that you are going to implement authentication in Zend Framework application but you can use Zend_Auth independently in any PHP application.

Let’s start, we will store users data in database, usual we have users table with username and password fields. We will also need to have function which task will be to return adapter from our users table  for username and password fields. Note that we store password using MD5 decryption, so we will need to set setCredentialTreatment to check password in MD5 format.

protected function _getAuthAdapter()
{
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)'); 

return $authAdapter;
}

 

Now we need to check given username and password so we create process function which is calling our adapter to see if there are given data in users table. This function will check is provided data is correct and if that the case we will return true, else we will return false.

    public function _process( $username, $password )
    {
    	// Get our authentication adapter and check credentials
    	$adapter = $this->_getAuthAdapter();
    	$adapter->setIdentity($username);
    	$adapter->setCredential($password);

    	$auth = Zend_Auth::getInstance();
    	$result = $auth->authenticate($adapter);
    	if ($result->isValid()) {
    		$user = $adapter->getResultRowObject();
    		$auth->getStorage()->write($user);
    		return true;
    	}
    	return false;
    }

 
And finally there is only one step left, we need to call above created process function  from our controller which we use to process user credentials, for example:

public function indexAction()
    {
        $this->_helper->layout->disableLayout();

        if ($this->getRequest()->isPost())
        {
        	$users = new Model_Users();
        	$alpha = new Zend_Filter_Alpha();
        	$params =  $this->getRequest()->getParams();

        	$username = $this->view->username = $params['username'];
        	$pass = $this->view->pass = $params['pass'];

        	if( $pass == '' || $username == '')
        	{
        		$this->view->message = "";
                } 

        	if ( !$this->_process( $username, $pass ) )
        	{
        		$this->view->message = '<p class="errors">Wrong username or password.</p>';
        		return ;
        	}
        	else
        	{
        		$this->_helper->redirector('dashboard', 'index');
        	}

 

And that’s it, very simple. If you have any comment or question about zend auth login issue, please be free to leave a comment or contact me.

 

No related posts.

9cc0b95266b4bf2dff43576c831427e3

24. April 2012 by admin
Categories: PHP | Tags: | Leave a comment

Leave a Reply

Required fields are marked *

*