cakephp 2记住我自动登录
我正在尝试设置它,以便当用户登录时,有一个“记住我”复选框,如果选中该复选框,将写入cookie以记住用户的电子邮件和密码。在“我的用户控制器登录”功能中如下所示:cakephp 2记住我自动登录,php,cakephp,Php,Cakephp,我正在尝试设置它,以便当用户登录时,有一个“记住我”复选框,如果选中该复选框,将写入cookie以记住用户的电子邮件和密码。在“我的用户控制器登录”功能中如下所示: public function login() { if (!empty($this->data)) { if ($this->Auth->login()) { $userId = $this->Auth->user('id');
public function login() {
if (!empty($this->data)) {
if ($this->Auth->login()) {
$userId = $this->Auth->user('id');
if(!empty($this->data['User']['remember'])) {
$user = $this->User->find('first', array('conditions'=>array('id'=>$userId), 'recursive'=>-1, 'fields'=>array('email', 'password')));
$this->Cookie->write('User', $user['User']);
}
... etc etc ...
在cakephp 1.x中,我已经实现了这一点,因此在我的AppController的beforefilter中,我只需查找cookie并尝试如下登录:
//try to auto login a users
if($this->Auth->user() == null) {
$user = $this->Cookie->read('User');
if(!empty($user)) {
$this->Auth->login($user);
}
}
但现在这似乎不起作用。我想是因为从我读到的,如果你把任何东西传递给登录函数,它会返回true。为了正确登录,我需要将cookie的内容发布到登录函数
是这样吗?有没有比尝试在某处创建表单并通过大量重定向让其发布信息更简单的方法
我还尝试将cookie信息添加到$this->request->data数组中并尝试登录,但也没有成功:(
有人能帮我吗?一定有更简单的方法!试试这个
在登录ctp文件中保留此复选框
<php echo $this->Form->checkbox('remember_me',array("id" => "id_remember_me",'name'=>'remember_me','label' => false)); ?>
<span onclick="$('#id_remember_me').attr('checked',true);"><?php echo __('Remember my details')?></span>
试试这个
在登录ctp文件中保留此复选框
<php echo $this->Form->checkbox('remember_me',array("id" => "id_remember_me",'name'=>'remember_me','label' => false)); ?>
<span onclick="$('#id_remember_me').attr('checked',true);"><?php echo __('Remember my details')?></span>
顺便说一句,我应该指出。我对cakephp有点陌生,我也用谷歌搜索过stackoverflow,但我找不到任何关于cakephp2.x的教程,从我读到的内容来看,有很大的不同,我希望你不要在你的cookie中存储原始密码……顺便说一下,我应该指出。我对cakephp有点陌生,我也用谷歌搜索过搜索了stackoverflow,但我找不到任何关于cakephp2.x的教程,从我读到的内容来看,有很大的不同,我希望您不要在cookie中存储原始密码。。。
code in the controller where the $this->Auth->login() is..
//Setting up the expiry time
$year = time() + 31536000;
if($this->request->data['remember_me']) {
// Creating a cookie in the name of remember_me with the username for the time which was set
setcookie('remember_me', $this->request->data['User']['username'], $year);
}
else if(!$this->request->data['remember_me']) {
if(isset($_COOKIE['remember_me'])) {
$past = time() - 100;
setcookie('remember_me', 'gone', $past);
}
}