Rss Feed
  1. Here table used is merchantmaster, model name is Merchantmaster , controller name is Merchantmasters.

    In Model add

       function validateLogin($data)
        {
          //$uname = $this -> query("select username, password from merchantmasters where username = '$u';");
           //print_r($uname);

            $user = $this->find(array('username' => $data['username'], 'password' => $data['password']), array('id', 'username'));
            //print_r($user);
            if(empty($user) == false)
                return $user['Merchantmaster'];
            return false;
        }

    In controller add

    function beforeFilter() {
            parent::beforeFilter();
           $this->__validateLoginStatus();
     }
       
        function login()
        {
            if(empty($this->data) == false)
            {
                if(($user = $this->Merchantmaster->validateLogin($this->data['Merchantmaster'])) == true)
                {
                    $this->Session->write('merchantmaster', $user);
                    $this->Session->setFlash('You\'ve successfully logged in.');
                    $this->redirect('index');
                    exit();
                }
                else
                {
                    $this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
                    $this->redirect('login');
                    exit();
                }
            }
        }
       
        function logout()
        {
            $this->Session->destroy('merchantmaster');
            $this->Session->setFlash('You\'ve successfully logged out.');
            $this->redirect('login');
        }


      
        function __validateLoginStatus()
      {
            if($this->action != 'login' && $this->action != 'logout')
            {
                if($this->Session->check('merchantmaster') == false)
                {
                    $this->redirect(array('action' =>'login'));
                    $this->Session->setFlash('The URL you\'ve followed requires you login.');
                }
            }
        }

    In view add

    <div class="row">
                <div class="span12">
                    <?php
                      echo $this->Layout->sessionFlash();
                    ?>
                </div>
            </div>
            <?php
        echo $form->create('Merchantmaster', array('action' => 'login'));
        echo $form->input('username');
       echo $form->input('password');
       echo $form->end('Login');
      
    ?>

  2. 0 comments:

    Post a Comment