Akrabat login class with small modification


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class Auth_IndexController extends Zend_Controller_Action
  4. {
  5.  
  6. protected $_db;
  7.  
  8. public function init()
  9. {
  10. $this->_db = Zend_Db_Table::getDefaultAdapter();
  11. }
  12.  
  13. public function indexAction()
  14. {
  15. # $db = Zend_Db_Table::getDefaultAdapter();
  16. # $db->getConnection();
  17. #
  18. # Zend_Debug::dump($db->fetchAll("SELECT * FROM users"));
  19. }
  20.  
  21. public function loginAction()
  22. {
  23. $form = new Form_Login();
  24. $form->submit->setLabel('Login');
  25. $this->view->form = $form;
  26. // Check if there's request post
  27. if ($this->getRequest()->isPost()) {
  28. // Takes data
  29. $formData = $this->getRequest()->getPost();
  30. if ($form->isValid($formData)) {
  31. $username = Zend_Filter::get($this->getRequest()->getPost('username'), 'StripTags');
  32. $password = Zend_Filter::get($this->getRequest()->getPost('password'), 'StripTags');
  33. $authAdapter = new Zend_Auth_Adapter_DbTable($this->_db);
  34. $authAdapter->setTableName('users');
  35. $authAdapter->setIdentityColumn('username');
  36. $authAdapter->setCredentialColumn('password');
  37.  
  38. // get the values
  39. $authAdapter->setIdentity($username);
  40. $authAdapter->setCredential($password);
  41.  
  42. $auth = Zend_Auth::getInstance();
  43. $result = $auth->authenticate($authAdapter);
  44.  
  45. if ($result->isValid()) {
  46. $data = $authAdapter->getResultRowObject(null, 'password');
  47. $auth->getStorage()->write($data);
  48. $this->_redirect('/');
  49. } else {
  50. // failure give the message
  51. $this->view->message = "Login failed, try again!";
  52. }
  53.  
  54. } else {
  55. // SHow what's wrong
  56. $form->populate($formData);
  57. }
  58. }
  59. }
  60.  
  61.  
  62. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.