Advertisement
gareth126

login auth

Nov 23rd, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. class Application_Model_Authenticate
  4. {
  5.  
  6.     function __construct(){
  7.    
  8.     }
  9.  
  10.    
  11.     public function checkCreds($username, $password)
  12.     {
  13.         $db = Zend_Db_Table::getDefaultAdapter();
  14.         $authAdapter = new Zend_Auth_Adapter_DbTable($db);
  15.        
  16.         $authAdapter
  17.             ->setTableName('users')
  18.             ->setIdentityColumn('username')
  19.             ->setCredentialColumn('password')
  20.             ->setIdentity($username)
  21.             ->setCredential($password)
  22.             ->setCredentialTreatment('MD5(?)');
  23.            
  24.         $result = $authAdapter->authenticate();
  25.        
  26.         return $result->isValid();
  27.        
  28.     }
  29.  
  30.    
  31. }
  32.  
  33.  
  34. //login view
  35. <br /><br />
  36. <div id="view-content">
  37.     <p>Login to the app!</p>
  38.     <form action="login" method="post" enctype="application/x-www-form-urlencoded">
  39.         <fieldset>
  40.             <legend>Login!</legend>
  41.             <dl>
  42.                 <dt><label for="name">Your name:</label></dt>
  43.                 <dd><input type="text" name="name" value="<?php $this->nameValue; ?>" /></dd>
  44.             </dl>
  45.             <dl>
  46.                 <dt><label for="password">Your Password:</label></dt>
  47.                 <dd><input type="password" name="password" value="" /></dd>
  48.             </dl>
  49.             <dl>
  50.                 <dt><label for="submit">Login:</label></dt>
  51.                 <dd><input type="submit" name="submit" value="Login" /></dd>
  52.             </dl>
  53.         </fieldset>
  54.     </form>
  55.    
  56.    
  57.     <a href="/users/logout" title="logout">Logout</a>
  58.    
  59.     <br />
  60.     <?php
  61.     if ($this->auth){
  62.         echo 'logged in correctly!';
  63.     }else{
  64.         echo 'not logged in!';
  65.     }
  66.     ?>
  67.    
  68. </div>
  69.  
  70. //users controller
  71.  public function loginAction()
  72.     {
  73.         if ($this->getRequest()->isPost() )
  74.         {
  75.    
  76.             $username = $this->getRequest()->getPost('name');
  77.             $password = $this->getRequest()->getPost('password');
  78.            
  79.             $authObject = new Application_Model_Authenticate();
  80.             $loginStatus = $authObject->checkCreds($username, $password);
  81.            
  82.             $this->view->auth = $loginStatus;
  83.             $this->view->nameValue = $username;
  84.         }
  85.    
  86.     }
  87.    
  88.     public function logoutAction(){
  89.         Zend_Auth::getInstance()->clearIdentity();
  90.         $this->_helper->redirector('login');
  91.     }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement