繁体   English   中英

注册cakephp后,电子邮件中的帐户激活链接

[英]Account activation link in email after registration cakephp

我正在使用cakephp 2.6。 成功注册后,我需要在电子邮件中发送帐户激活链接,如果用户单击激活链接,则我数据库的状态字段将更新为1。

我的电子邮件将正确无误,但我不知道如何通过获取lastinsertid作为电子邮件中的唯一标识符来发送电子邮件中的链接以及单击后更新帐户。

请帮助我。

function add() {       
        $this->layout = "layout_login";      
            $this->pageTitle = "Add Admin";
            $this->set("pageTitle", "Add Admin");      
        if (!empty($this->request->data)) {           
            $this->User->set($this->data['User']);
            $isValidated = $this->User->validates();
            if ($isValidated) {   
               // Generating UUID
                $this->data['User']['activation_key'] = $activation_key = String::uuid();
                $this->User->save($this->data, array('validate' => false)); 
                $this->Session->setFlash(__('Please check your email for account verification.')); 
                    $subject = "Account Activation link By Kaya Dispatch";                  

                    $to = trim($this->request->data['User']['email']);
                    $this->Email->sendAs = 'html';
                    $this->Email->from = 'luckybajpai87@gmail.com';
                    $this->Email->to = $to;
                    $this->Email->subject = $subject;   
                    $activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]);
                    $message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>";
                    $message .= "Your account has been created successfully by Administrator.<br/>";
                    $message .= "Please find the below details of your account: <br/><br/>";
                    $message .= "<b>Activate your account by clicking on the below url:</b> <br/>";
                    $message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>";
                    $message .= "<br/>Thanks, <br/>Support Team";
                    $this->Email->send($message);

                    }            
                } 
            } 

以下是我的帐户激活码

function activate($activation_key) {
        $userData = $this->User->find('first', array(
        'conditions' => array(
                'User.activation_key' => $activation_key,
                'User.status' => 0
        )
    ));
    if( !empty($userData)){
           if ($userData['User']['status'] == 0)
            {
                $activeStatus = 1;
            } 
    $status = $this->User->updateAll(array('User.status' => $activeStatus), array('User.id' => $id));
    if ($status)
            {   $this->Session->setFlash(__('Status updated successfully'));

            } else
            {   $this->Session->setFlash(__('Something went wrong, please try again.'));

            }
             $this->redirect(array('controller' => 'Users', 'action' => 'index'));

    }
}

嗯,这是合乎逻辑的,我的意思是,这完全取决于您要执行的操作。 我在写我的路:)

users表中新建一列,即activation_key ,然后在将其插入表之前进行设置。 像这样:

if ($isValidated) {

    $password = $this->data['User']['password'];
    $this->data['User']['password'] = md5($this->data['User']['password']);

    // Generating UUID
    $this->data['User']['activation_key'] = $activation_key = String::uuid();


    $this->User->save($this->data, array('validate' => false));               
    $this->Session->setFlash("<div class='success-message flash notice'>Admin has been created successfully.</div>");
    $subject = "Account Activation link send on your email";
    $name = $this->request->data['User']['fname'] . " " . $this->request->data['User']['lname'];
    $to = trim($this->request->data['User']['email']);
    $this->Email->sendAs = 'html';
    $this->Email->from = 'luckybajpai87@gmail.com';
    $this->Email->to = $to;
    $this->Email->subject = $subject;

    $activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]);

    // Always try to write clean code, so that you can read it :) :

    $message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>";
    $message .= "Your account has been created successfully by Administrator.<br/>";
    $message .= "Please find the below details of your account: <br/><br/>";
    $message .= "<b>First Name:</b> " . $this->data['User']['fname'] . "<br/>";
    $message .= "<b>Last Name:</b> " . $this->data['User']['lname'] . ((!empty($this->data['User']['phone'])) ? "<br/>";
    $message .= "<b>Phone:</b> " . $this->data['User']['phone'] : "") . "<br/>";
    $message .= "<b>Address:</b> " . $this->data['User']['address1'] . " " . $this->data['User']['address2'] . "<br/>";
    $message .= "<b>Email:</b> " . $this->data['User']['email'] . "<br/>";
    $message .= "<b>Username:</b> " . $this->data['User']['username'] . "<br/>";
    $message .= "<b>Password:</b> " . $password . "<br/>";

    $message .= "<b>Activate your account by clicking on the below url:</b> <br/>";
    $message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>";
    $message .= "<br/>Thanks, <br/>Support Team";
    $this->Email->send($message);

}

然后在您的function activate($activation_key='')将该激活密钥作为参数接收,并从这样的用户中查找:

// Finding user data from users table on behalf of activation key
// and Status should be 0 (deactivate). So that, a URL can be use only ONCE.
$userData = $this->User->find('first', array(
        'conditions' => array(
                'User.activation_key' => $activation_key,
                'User.status' => 0
        )
    ));

if( !empty($userData) ){
    // $userData as you have User's data update the status as 1 and set activation_key as empty "";
}else{
    // So, you don't find any user, it is an invalid request.
}

有时,Cakephp不允许您更新$this->data因为在此示例中,我们试图更改$this->data['User']['password']$this->data['User']['activation_key']值,因此您可以简单地存储$postData = $this->data ,然后将$postData用于插入和进一步的操作。

更新:请确认/调试String::uuid()是否为您正常工作,如果它不起作用,请尝试使用以下行来达到此目的:

// Generating activation_key
$this->data['User']['activation_key'] = $activation_key = time();

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM