Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. // ----- form -------------
  2.  
  3. <?php
  4.  
  5. class Application_Form_Login extends Zend_Form {
  6.  
  7. public function init() {
  8.  
  9. $this->setMethod('post');
  10.  
  11. $this->addElement('text', 'username', array(
  12. 'label' => 'Username: ',
  13. 'required' => TRUE,
  14. 'attribs' => array(
  15. 'size' => '20'
  16. ),
  17. 'validators' => array(
  18. new Zend_Validate_Alnum(),
  19. new Zend_Validate_StringLength(array('min' => 4, 'max' => 30))
  20. ),
  21. 'filters' => array('StripTags', 'StripNewLines')
  22. ));
  23.  
  24. $this->addElement('password', 'password', array(
  25. 'label' => 'Password: ',
  26. 'required' => TRUE,
  27. 'attribs' => array(
  28. 'size' => '20'
  29. ),
  30. 'validators' => array(
  31. new Zend_Validate_StringLength(array('min' => 8))
  32. ),
  33. 'filters' => array('StripTags', 'StripNewLines')
  34. ));
  35.  
  36. $this->addElement('submit', 'submit');
  37. }
  38.  
  39. }
  40.  
  41. // ------------ view -------------------
  42. <h2>User Login</h2>
  43. <p>To login to your account please enter your username and password below...</p>
  44. <?php if($this->loginMessage) echo '<p>'.$this->loginMessage.'</p>'; ?>
  45. <?php echo $this->form; ?>
  46.  
  47. //------------- controller --------------
  48. public function loginAction() {
  49. // action body
  50. $userForm = new Application_Form_Login();
  51. $this->view->form = $userForm;
  52. if ($this->_request->isPost() && $userForm->isValid($_POST)) {
  53.  
  54. $data = $userForm->getValues();
  55.  
  56. $bootstrap = $this->getInvokeArg('bootstrap');
  57. $db = $bootstrap->getResource('db');
  58. if ($db instanceof Zend_Db_Adapter_Abstract) {
  59. $db->getProfiler()->setEnabled(true);
  60. }
  61.  
  62. $auth = new Zend_Auth_Adapter_DbTable($db);
  63. $auth->setTableName('users')
  64. ->setIdentityColumn('username')
  65. ->setCredentialColumn('password')
  66. ->setIdentity($data['username'])
  67. ->setCredential($data['password']);
  68. $result = $auth->authenticate();
  69. if ($result->isValid()) {
  70. $userForm->populate($data);
  71. } else {
  72. $this->view->loginMessage = "Sorry, your username or password was incorrect: " . $data['username'] . "/" . $data['password'];
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement