// Getting the authentication adapter $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'MD5(CONCAT(?,salt))'); // Setting the credentials from the submitted form $authAdapter->setIdentity($username) ->setCredential($password); // Authenticate the user $result = $authAdapter->authenticate(); // Checking authentication result if ($result->isValid()) { // Authentication successful, set user identity $user = $authAdapter->getResultRowObject(null, 'password'); Zend_Auth::getInstance()->getStorage()->write($user); // Redirect to user's dashboard $this->_helper->redirector('dashboard'); } else { // Authentication failed, display error message $this->view->errorMessage = 'Invalid Username or Password'; }
// Getting the authentication adapter $authAdapter = new Zend_Auth_Adapter_Http($options); // Authenticate the user $result = $authAdapter->authenticate(); // Checking authentication result if ($result->isValid()) { // Authentication successful, set user identity $user = $authAdapter->getIdentity(); Zend_Auth::getInstance()->getStorage()->write($user); } else { // Authentication failed, display error message header('WWW-Authenticate: Basic realm="My Website"'); header('HTTP/1.0 401 Unauthorized'); echo 'Authentication required'; exit; }This code creates an authentication adapter using the Zend_Auth_Adapter_Http class which supports HTTP basic authentication. The adapter retrieves the provided credentials and checks them with the given credentials. If the authentication succeeds, it sets the user identity, otherwise it returns a 401 error. Zend_Auth is part of the Zend Framework, which is a package library that provides a wide range of classes and tools for PHP web development. Some of the other classes included in the Zend Framework are Zend_Db, Zend_Log, Zend_Paginator, and Zend_Cache.