CakePHP:AUTH-> LOGIN()...是否用户/传递错误...它重定向到索引页面

时间:2014-01-06 12:24:39

标签: cakephp authentication redirect components

我的问题是:'当我输入错误的用户名 - 密码组合时,它仍然会将我重定向到索引页面,而它应该再次重定向到登录页面'..有什么问题?Pl帮我...我我在看我的代码...

这里是AppController:

class AppController extends Controller {


         public $components=array('DebugKit.Toolbar',
         'Session','Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'login'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'index')
        ));


        public function beforeFilter(){

            $this->Auth->allow('index','register');
        }
}

这里是UsersController:

class UsersController extends AppController
{
    public $name='Users';
    public $uses=array('user');
    public $helpers = array('Html', 'Form','Session');

        public function beforeFilter() {
         parent::beforeFilter();
         }
        public function index()
        {

        }
        public function login() {
        if ($this->request->is('post')) {
            /* login and redirect to url set in app controller */
            if ($this->Auth->login($this->request->data)) {
                 $this->Session->setFlash(__('Successful!!!'));
                 $this->Session->write('user',$this->data['user']['username'],time()+3600);
                return $this->redirect(array('action'=>'index'));
            }
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
      // else{ echo "fail";exit;}
    }

    public function logout() {
         /* logout and redirect to url set in app controller */
         $this->Session->destroy();
        return $this->redirect($this->Auth->logout());
    }
public function register() {
        if ($this->request->is('post')) {
            $this->user->create();
            if ($this->user->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('controller' => 'users','action' => 'index'));
            }
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }


      }
}

这是用户模型:

<?php
class User extends AppModel
{
       var  $name='User';
        var $validate=array(
        'username'=> array(
                'rule'=>'notEmpty',
                    'message'=>'Enter Your USername'),
        'password'=>array(
                'rule'=>'notEmpty',
                'message'=>'Enter your Password'
                        ),
        'Confirm_password'=>array(
                'rule'=>'match',
                'message'=>'password not match, try again'
                        )

        );
        public function match(){
            if($this->data['user']['password']===$this->data['user']['Confirm_password'])
            {
                return true;
            }
            return false;

        }
         public function beforeSave($options = array()) {

    /* password hashing */    
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
    }

}

?>

这是我的登录ctp:

<h2>LOGIN-MYSITE</h2>
<?php echo $this->Form->create('user',array('action'=>'login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('LOGIN');


?>

1 个答案:

答案 0 :(得分:0)

中删除'index'
$this->Auth->allow('index','register');

 $this->Auth->allow('register');