duanchao1002 2011-10-30 17:06
浏览 27
已采纳

帐户激活的最佳方式

I'm trying to create an account register page with CakePHP 2.0 where user needs to activate it's new account by clicking on a link in the email he's received after insert username, email and password.

My question is how can I set an activation code inside the user record.

I thought to create a table field named activation_code and then to store an hashed version of the username to be sure the user can activate itself by clicking the email link with the activation key.

All the procedure is done but I don't know how can I set the activation_code inside the $data['User'] object and It's not clear for me if this is a good usage of the MVC framework or I should make it in a different way.

During the user registration action I've done this but I get an error when I try to create 'activation_code' dynamically:

// from the UserController class
public function register () {
    if (!empty($this->data)) {
        if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
            // here is where I get the error
            $this->data['User']['activation_key'] = AuthComponent::password($this->data['User']['email']);
            $this->User->create();
            if ($this->User->save($this->data)) {
                // private method
                $this->registrationEmail ($this->data['User']['email'], $this->data['User']['username']);
                $this->redirect(array('controller'=>'users', 'action'=>'registration', 'success'));
            }
        }
    }
}

Obviously the activation_key is an empty field inside my database.

So how can I create a filed dynamically from the controller?

  • 写回答

2条回答 默认 最新

  • dongxi1965 2011-11-05 11:15
    关注

    I've solved the problem with the method Model::set(), so:

    public function register () {
        if (!empty($this->data)) {
            if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
                $this->User->create();
                // I've used set method
                $this->User->set('activation_key', AuthComponent::password($this->data['User']['email']));
                if ($this->User->save($this->data)) {
                    $this->registrationEmail ($this->data['User']['email'], $this->data['User']['username']);
                    $this->redirect(array('controller'=>'users', 'action'=>'registration', 'success'));
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向