2.4.2 中的登录脚本不起作用 [英] Login Script in 2.4.2 is not working
问题描述
我是 cakephp 的新手.我在登录时遇到问题.使用错误的名称和密码重定向到登录主页.
I am new to cakephp. I have a problom while login. With wrong name and password redirects to login home page.
public function login() {
$this->layout = 'admin-login';
if ($this->request->is('post')) {
if ($this->Auth->login($this->request->data)) {
return $this->redirect($this->Auth->redirectUrl())
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'loginAction' => array('controller' => 'users', 'action' => 'login')
)
);
public function beforeFilter() {
$this->Auth->allow("login");
//$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array(
'User.is_active' => 1
)
)
);
}
public function isAuthorized($user) {
return true;
}
登录.ctp
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->submit(__('Submit');
echo $this->Form->end();
当我填写错误的用户名时密码 &点击提交按钮它重定向到主页,谢谢.
When i fill the wrong username & password & click on submit button it redirect to home page, Thanks.
推荐答案
您使用的是AuthComponent::login()
错误,您只应该将数据传递给它,以防您想手动登录用户,即没有自动身份验证.
You are using AuthComponent::login()
wrong, you are only supposed to pass data to it in case you want to manually login a user, ie without automatic authentication.
如果您想使用组件身份验证功能,只需调用 $this->Auth->login()
If you want to use the components authentication functionality just call $this->Auth->login()
在 2.x 中 $this->Auth->login($this->request->data)
将使用发布的任何数据登录用户,而在 1.3 中$this->Auth->login($this->data)
会先尝试识别用户,成功后才登录.
In 2.x
$this->Auth->login($this->request->data)
will log the user in with whatever data is posted, whereas in 1.3$this->Auth->login($this->data)
would try to identify the user first and only log in when successful.
这篇关于2.4.2 中的登录脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!