Authentication 哈希密码和身份验证组件
我正在尝试登录我的Authentication 哈希密码和身份验证组件,authentication,hash,login,cakephp-2.0,Authentication,Hash,Login,Cakephp 2.0,我正在尝试登录我的CakePHP2.0应用程序,但总是出现登录错误。 在官方和官方网站上,我已经阅读了如何散列密码,但我仍然得到了登录错误,下面是我如何做到的: // Users Model public function beforeSave ($options = array ()) { $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
CakePHP2.0
应用程序,但总是出现登录错误。
在官方和官方网站上,我已经阅读了如何散列密码,但我仍然得到了登录错误,下面是我如何做到的:
// Users Model
public function beforeSave ($options = array ()) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
// Users Controller
public $components = array ('Acl', 'Session',
'Auth' => array (
'authenticate' => array (
// login e logout sono di default i seguenti controller e views
// 'loginRedirect' => array ('controller' => 'users', 'action' => 'login'),
// 'logoutRedirect' => array ('controller' => 'users', 'action' => 'logout'),
'Form' => array (
'fields' => array (
// il valore default
'username' => 'email'
),
'scope' => array (
'User.active' => 1
)
)
),
'authError' => 'Login error message I get'
));
public function login () {
if ($this->request->is('post')) { // if the request came from post data and not via http (useful for security)
// the password is hashed in User Model in beforeSave method as read on documentation
// debug ($this->data);
if ($this->Auth->login()) {
$id = $this->Auth->user('id');
return $this->redirect(array('controller'=>'users', 'action'=>$id, $this->Auth->user('username')));
} else {
$this->Session->setFlash('Login error message', 'default', array(), 'auth');
}
}
}
我认为:
// the view login.ctp
echo $this->Form->text('User.email', array('id'=>'email', 'value'=>'your@email.com'));
echo $this->Form->password('User.password', array('id'=>'password', 'value'=>'password'));
如果我尝试调试数据,我会得到以下结果:
// in the controller
debug($this->data);
// in the view
Array
(
[User] => Array
(
[email] => the@email.com
[password] => thepass // not hashed
)
)
我无法登录,因为我总是收到登录错误消息
。我怎样才能修好它 Vitto
(1) 将显示哪个错误消息
(2) 为了记录在案,请确保在版面上打印sessionFlash
echo $this->Layout->sessionFlash();
(3) 您是如何设置身份验证组件的?例如,在我的AppController中,我设置了以下方式:
public $components = array(
'Session',
'Cookie',
'Acl',
/**
* Default is authorize option is ActionsAuthorize.
* In this case, system uses AclComponent to check for permissions on an action level.
* learn more: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization
*/
'Auth'=> array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'password')
)
)
)
);
(4) 最后,我相信(必须确保)不需要手动散列密码来执行登录。至少,在正确配置后,此代码对我有效:
if ($this->request->is('post')) {
if ($this->Auth->login()) {
// recirect stuffs
谢谢你的帮助1)我已经更新了问题中的错误2) 如果我试图打印sessionFlash,我会得到:致命错误:在/public_html/site.com/app/View/Layouts/default.ctp | 3)中对非对象调用成员函数sessionFlash()!谢谢(2)并确保已在控制器或AppController上的
$helpers
数组中设置了helper布局public$helpers=array(/*others*/,'Layout',/*others*/)
;也许你说的是:
这是有效的,我用它来获取错误我说的关于这个错误的错误致命错误:在/public\u html/site.com/app/View/Layouts/default.ctp中对非对象调用成员函数sessionFlash()。如果您试图调用$this->Layout->sessionFlash()代码>没有设置布局
helper,将无法工作。您是否已尝试检查是否($this->Session->check('Auth.User.id'){…}
以了解您是否至少已成功登录?您是在谈论CakePHP 2.0吗<代码>$this->Layout->sessionFlash()
在2.0文档中不存在,我认为它是用$this->Session->flash('auth')更改的代码>用于调试登录和会话异常