我是快速开发框架的新手,目前正在做一个关于Cakephp的项目。我在应用程序上建立会话时遇到问题。我使用了登录功能,但它不接受我输入的凭据,并返回不正确的凭据的闪存消息。我尝试以不同的方式更改函数,但很明显它没有建立会话。请帮忙。
这是相关代码。
用户控制器.php
public function login() {
if ($this->request->is('post')) {
if ( $this->request->is( 'post' ) ) {
if ( $this->Auth->login() ) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
}
}
应用控制器.php
public $components = array('Session', 'Auth');
您的应用程序控制器似乎缺少$components
中的一些信息。
试试这个:
class AppController extends Controller {
public $components = array(
'Cookie',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => '**YOUR CONTOLLERr**',
'action' => '**YOUR ACTION**'
),
'logoutRedirect' => array(
'controller' => '**YOUR CONTOLLERr**',
'action' => '**YOUR ACTION**',
'home'
),
'loginAction' => array(
'controller' => '**YOUR CONTOLLERr**',
'action' => '**YOUR ACTION**'
),
'authError' => 'Access Denied!',
'loginError' => 'Invalid user and password',
'authorize' => array('Controller'),
'authenticate' => array('Form' => array(
'userModel' => 'User',
'fields' => array(
'username' => '**LOGIN FIELD**',
'password' => '**PASSWORD FIELD**'
)
)
),
)
);
}
在AppController中设置Session
非常重要,这样您就可以建立它。检查PasswordHash也很重要,因为Cakephp只有在存储在数据库中的密码被哈希时才验证凭据。
希望它对你有帮助。
我在您的"相关代码"中看不到任何内容,您可以将获得的数据与登录中的数据库进行比较,只需使用调试,因此:
public function login() {
if ($this->request->is('post')) {
debug($this->request->data);
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
//$this->Session->setFlash(('Your username or password was incorrect.'));
}
}
}
有了这个,我们可以获得更多信息,您可以告诉我什么显示以及这些数据在数据库中是否相同。