Return to Snippet

Revision: 14885
at June 16, 2009 04:19 by staar2


Initial Code
<?php

class Auth_IndexController extends Zend_Controller_Action
{

	protected $_db;

    public function init()
    {
		$this->_db = Zend_Db_Table::getDefaultAdapter();
    }

    public function indexAction()
    {
#		$db = Zend_Db_Table::getDefaultAdapter();
#		$db->getConnection();
#		
#		Zend_Debug::dump($db->fetchAll("SELECT * FROM users"));
    }
    
    public function loginAction()
    {
        $form = new Form_Login();
		$form->submit->setLabel('Login');
		$this->view->form = $form;
		// Check if there's request post
		if ($this->getRequest()->isPost()) {
			// Takes data			
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$username = Zend_Filter::get($this->getRequest()->getPost('username'), 'StripTags');
				$password = Zend_Filter::get($this->getRequest()->getPost('password'), 'StripTags');
				$authAdapter = new Zend_Auth_Adapter_DbTable($this->_db);
				$authAdapter->setTableName('users');
				$authAdapter->setIdentityColumn('username');
				$authAdapter->setCredentialColumn('password');
			
				// get the values
				$authAdapter->setIdentity($username);
				$authAdapter->setCredential($password);
			
				$auth = Zend_Auth::getInstance();
				$result = $auth->authenticate($authAdapter);
				
				if ($result->isValid()) {
					$data = $authAdapter->getResultRowObject(null, 'password');
					$auth->getStorage()->write($data);
					$this->_redirect('/');
				} else {
					// failure give the message
					$this->view->message = "Login failed, try again!";
				}
				
			} else {
				// SHow what's wrong
				$form->populate($formData);
			}
		}
    }


}

Initial URL


Initial Description


Initial Title
Akrabat login class with small modification

Initial Tags


Initial Language
PHP