Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //the view
- ?>
- <br /><br />
- <div id="view-content">
- <p>Login to the app!</p>
- <?php echo $this->loginForm; ?>
- <a href="/users/logout" title="logout">Logout</a>
- <br />
- <?php
- if ($this->auth){
- echo 'logged in correctly!';
- }else{
- echo 'not logged in!';
- }
- ?>
- </div>
- <?php
- //the login class
- class Application_Form_Login extends Zend_Form
- {
- function init(){
- $this->setAction('/users/login')
- ->setMethod('post');
- // Create and configure username element:
- $username = $this->createElement('text', 'name');
- $username->addValidator('alnum')
- ->addValidator('regex', false, array('/^[a-z]+/'))
- ->addValidator('stringLength', false, array(6, 20))
- ->setRequired(true)
- ->addFilter('StringToLower');
- // Create and configure password element:
- $password = $this->createElement('password', 'password');
- $password->addValidator('StringLength', false, array(6))
- ->setRequired(true);
- // Add elements to form:
- $this->addElement($username)
- ->addElement($password)
- ->addElement('submit', 'login', array('label' => 'Login'));
- }
- }
- //the controller action
- public function loginAction()
- {
- $loginForm = new Application_Form_Login();
- $this->view->loginForm = $loginForm;
- if ( ($this->getRequest()->isPost('name') != '') && $loginForm->isValid($_POST) )
- {
- $username = $loginForm->getValue('name');
- $password = $loginForm->getValue('password');
- $authObject = new Application_Model_Authenticate();
- $loginStatus = $authObject->checkCreds($username, $password);
- $this->view->auth = $loginStatus;
- $this->view->nameValue = $username;
- }
- }
- //the authentication class
- class Application_Model_Authenticate
- {
- function __construct(){
- }
- public function checkCreds($username, $password)
- {
- $db = Zend_Db_Table::getDefaultAdapter();
- $authAdapter = new Zend_Auth_Adapter_DbTable($db);
- $authAdapter
- ->setTableName('users')
- ->setIdentityColumn('username')
- ->setCredentialColumn('password')
- ->setIdentity($username)
- ->setCredential($password)
- ->setCredentialTreatment('MD5(?)');
- $result = $authAdapter->authenticate();
- return $result->isValid();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement