<? Online-PHP::Tutorials ?>

« Back

  • ZF: Create the loginAction - receive login form, and authenticate.

    
    

    Example of a controller action for authenticating the request from the login form

    
        public function loginAction()
        {
            // check that this is POST request
            $req = $this->getRequest();
            if (!$req->isPost()) {
                $this->_redirect('/');
                return;
            }
    
            // get the database resource
            $db = Zend_Registry::getInstance()->get('db');
    
    
            // get POST parameters
            $login = $this->getParam('login');
            $password = $this->getParam('password');
    
            // define the adapter
            $authAdapter = new Zend_Auth_Adapter_DbTable($db);
            $authAdapter->setTableName('Users');
                ->setIdentityColumn('username');
                ->setCredentialColumn('password');
                ->setIdentity($login);
                ->setCredential($password);
                ->setCredentialTreatment('MD5(?)');
    
            // authenticate
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);
            if ($result->isValid()) {
                $data = $authAdapter->getResultRowObject(null, 'password'); // without password
                $auth->getStorage()->write($data);
            }
    
            $this->_redirect('/');
        }