Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----- form -------------
- <?php
- class Application_Form_Login extends Zend_Form {
- public function init() {
- $this->setMethod('post');
- $this->addElement('text', 'username', array(
- 'label' => 'Username: ',
- 'required' => TRUE,
- 'attribs' => array(
- 'size' => '20'
- ),
- 'validators' => array(
- new Zend_Validate_Alnum(),
- new Zend_Validate_StringLength(array('min' => 4, 'max' => 30))
- ),
- 'filters' => array('StripTags', 'StripNewLines')
- ));
- $this->addElement('password', 'password', array(
- 'label' => 'Password: ',
- 'required' => TRUE,
- 'attribs' => array(
- 'size' => '20'
- ),
- 'validators' => array(
- new Zend_Validate_StringLength(array('min' => 8))
- ),
- 'filters' => array('StripTags', 'StripNewLines')
- ));
- $this->addElement('submit', 'submit');
- }
- }
- // ------------ view -------------------
- <h2>User Login</h2>
- <p>To login to your account please enter your username and password below...</p>
- <?php if($this->loginMessage) echo '<p>'.$this->loginMessage.'</p>'; ?>
- <?php echo $this->form; ?>
- //------------- controller --------------
- public function loginAction() {
- // action body
- $userForm = new Application_Form_Login();
- $this->view->form = $userForm;
- if ($this->_request->isPost() && $userForm->isValid($_POST)) {
- $data = $userForm->getValues();
- $bootstrap = $this->getInvokeArg('bootstrap');
- $db = $bootstrap->getResource('db');
- if ($db instanceof Zend_Db_Adapter_Abstract) {
- $db->getProfiler()->setEnabled(true);
- }
- $auth = new Zend_Auth_Adapter_DbTable($db);
- $auth->setTableName('users')
- ->setIdentityColumn('username')
- ->setCredentialColumn('password')
- ->setIdentity($data['username'])
- ->setCredential($data['password']);
- $result = $auth->authenticate();
- if ($result->isValid()) {
- $userForm->populate($data);
- } else {
- $this->view->loginMessage = "Sorry, your username or password was incorrect: " . $data['username'] . "/" . $data['password'];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement