CakePHP 2.0帐户激活后自动登录 [英] CakePHP 2.0 Automatic Login after Account Activation

查看:81
本文介绍了CakePHP 2.0帐户激活后自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究新项目的用户管理组件。
计划为:


  1. 用户在页面上进行注册时,帐户数据(用户名,通行证,电子邮件)的数量最少li>
  2. 用户收到一封带有激活链接的电子邮件以激活帐户

  3. 用户单击链接并激活其帐户

  4. 系统在激活后自动登录用户,并将其重定向到带有帐户信息的仪表板(上次登录,用户名等)。

但是自动登录存在一些问题。这是我使用的代码的一部分:

 <?php 
...
/ /将userstatus设置为 active并删除元信息 activation_key
// //然后自动登录
$ this-> User-> id = $ id;
$ this-> User-> saveField('modified',date('Y-m-d H:i:s'));
$ this-> User-> saveField('status',1);

// $ this-> User-> deleteActivationKey ....

$ this-> Auth-> login($ this-> User- > read());
$ this->会话-> setFlash(__(成功激活的帐户。您现在已登录。));

$ this-> User-> saveField(’last_login’,date(’Y-m-d H:i:s’));

$ this-> redirect(array(‘controller’=>’pages’));
...

到目前为止,该方法一直有效,直到您想要获取有关已记录日志的信息



我们在AppController-> beforeRender中使用此功能,以在整个用户范围内使用用户信息:

  $ this-> set('auth',$ this-> Auth-> user()); 

但是在执行自动登录操作后,我收到了未定义的索引通知。 (例如,通过在视图中访问$ auth ['id'])。 print_r()仅向我显示当前用户的用户名和哈希密码。
如果您手动登录,一切正常。



好像是会话有问题吗?我在做什么错?

解决方案

在测试了许多变体之后找到了解决方案。



现在可以使用:

  $ user = $ this-> User-> findById($ id); 
$ user = $ user [’User’];
$ this-> Auth-> login($ user);

不知道为什么,我想我已经尝试过这种方法了,但是没有用。 p>

I'm just working on the user management-component of our new project. The plan is:

  1. User registers on the page with minimal amount of account data (username, pass, email)
  2. User gets an email with an activation link to activate the account
  3. User clicks on the link and activates his account
  4. The system logs in the user after automatically after activation and redirects him to kind of a dashboard with account information (last login, hi "username", etc.)

But there are some problems with the auto login. this is the part of the code i use:

<?php
...
// set userstatus to "active" and delete meta information "activation_key"
// then automatically login
$this->User->id = $id;
$this->User->saveField('modified', date('Y-m-d H:i:s') );
$this->User->saveField('status', 1 );

// $this->User->deleteActivationKey ....

$this->Auth->login($this->User->read());
$this->Session->setFlash(__('Successfully activated account. You are now logged in.'));

$this->User->saveField('last_login', date('Y-m-d H:i:s') );

$this->redirect(array('controller' => 'pages'));
...

This works so far, until you want to get information about the logged in user with the user() function of the Auth Component.

We're using this in AppController->beforeRender, to have user information application wide:

$this->set('auth', $this->Auth->user());

but after that auto login action, i'm getting undefined index notices. (e.g. by accessing $auth['id'] in a view). print_r() shows me only the username and hashed password of the current user. If you login manually, everything works fine. it must be something with the automatic login after the account activation.

Seems to be a problem with the session? What am i doing wrong?

解决方案

Found a solution after testing many variations.

Works now with:

$user = $this->User->findById($id);
$user = $user['User'];
$this->Auth->login($user);

Don't know why, i thought i tried this way already and that did not work.

这篇关于CakePHP 2.0帐户激活后自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆