Cakephp 如何接收注册用户的确认码?
我正在应用本教程: 当我提交邮件时,它是这样发送的: 嘿,艾哈迈德,我们会让你立即启动并运行,但首先我们只需要你点击下面的链接确认你的用户帐户: http://localhost/cakenews/users/activate/24/8877ae4a 问题是我想创建文件users/active.ctp,以便在用户单击链接时接收激活码。但是我不知道要给活跃用户写什么Cakephp 如何接收注册用户的确认码?,cakephp,cakephp-1.3,cakephp-1.2,Cakephp,Cakephp 1.3,Cakephp 1.2,我正在应用本教程: 当我提交邮件时,它是这样发送的: 嘿,艾哈迈德,我们会让你立即启动并运行,但首先我们只需要你点击下面的链接确认你的用户帐户: http://localhost/cakenews/users/activate/24/8877ae4a 问题是我想创建文件users/active.ctp,以便在用户单击链接时接收激活码。但是我不知道要给活跃用户写什么 <?php # /controllers/users_controller.php # please note that no
<?php
# /controllers/users_controller.php
# please note that not all code is shown...
uses('sanitize');
class UsersController extends AppController {
var $name = 'Users';
// Include the Email Component so we can send some out :)
var $components = array('Email', 'Auth');
// Allow users to access the following action when not logged in
function beforeFilter() {
$this->Auth->allow('register', 'confirm', 'logout');
$this->Auth->autoRedirect = false;
}
// Allows a user to sign up for a new account
function register() {
if (!empty($this->data)) {
// See my previous post if this is forgien to you
$this->data['User']['password'] = $this->Auth->password($this->data['User']['passwrd']);
$this->User->data = Sanitize::clean($this->data);
// Successfully created account - send activation email
if ($this->User->save()) {
$this->__sendActivationEmail($this->User->getLastInsertID());
// this view is not show / listed - use your imagination and inform
// users that an activation email has been sent out to them.
$this->redirect('/users/register');
}
// Failed, clear password field
else {
$this->data['User']['passwrd'] = null;
}
}
}
/**
* Send out an activation email to the user.id specified by $user_id
* @param Int $user_id User to send activation email to
* @return Boolean indicates success
*/
function __sendActivationEmail($user_id) {
$user = $this->User->find(array('User.id' => $user_id), array('User.id','User.email', 'User.username'), null, false);
if ($user === false) {
debug(__METHOD__." failed to retrieve User data for user.id: {$user_id}");
return false;
}
// Set data for the "view" of the Email
$this->set('activate_url', 'http://' . env('SERVER_NAME') . '/cakenews/users/activate/' . $user['User']['id'] . '/' . $this->User->getActivationHash());
$this->set('username', $this->data['User']['username']);
$this->Email->to = $user['User']['email'];
$this->Email->subject = env('SERVER_NAME') . ' - Please confirm your email address';
$this->Email->from = 'email@gmail.com';
$this->Email->template = 'user_confirm';
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'email@gmail.com',
'password'=>'password',
);
$this->Email->sendAs = 'text'; // you probably want to use both :)
return $this->Email->send();
}
}
/**
* Activates a user account from an incoming link
*
* @param Int $user_id User.id to activate
* @param String $in_hash Incoming Activation Hash from the email
*/
function activate($user_id = null, $in_hash = null) {
$this->User->id = $user_id;
if ($this->User->exists() && ($in_hash == $this->User->getActivationHash()))
{
// Update the active flag in the database
$this->User->saveField('active', 1);
// Let the user know they can now log in!
$this->Session->setFlash('Your account has been activated, please log in below');
$this->redirect('login');
}
// Activation failed, render '/views/user/activate.ctp' which should tell the user.
}
?>
视图为cakenews\app\views\users\register.ctp
<fieldset>
<legend>Add a article</legend>
<?php
echo $form->create('User');
echo $form->input('username');
echo $form->input('password');
echo $form->input('email');
?>
</fieldset>
<?php echo $form->end('Post project'); ?>
添加一篇文章
在控制器操作中,激活
正在处理用户激活。如果用户被验证,它将重定向到登录操作,否则它将尝试呈现activate.ctp
文件。因此,在这种情况下,您可以向用户显示一条消息,说明您的注册无法完成。请再试一次。要为视图(activate.ctp)设置一些数据,可以使用控制器的set方法
如果您根本不想呈现activate.ctp文件,请将用户重定向到其他地方,告知注册无法完成。控制器操作中的
activate
正在处理用户激活。如果用户被验证,它将重定向到登录操作,否则它将尝试呈现activate.ctp
文件。因此,在这种情况下,您可以向用户显示一条消息,说明您的注册无法完成。请再试一次。要为视图(activate.ctp)设置一些数据,可以使用控制器的set方法
如果您根本不想呈现activate.ctp文件,请将用户重定向到其他地方,告知注册无法完成。当我单击链接时,我在UsersController中看到此消息缺少方法错误:在controller UsersController中未定义activate操作错误为(((UsersController错误中缺少方法:未在控制器UsersController错误中定义激活操作:在文件:app\controllers\users\u controller.中创建UsersController::activate())单击链接时,我看到以下消息:UsersController错误中缺少方法:控制器UsersController中未定义操作激活错误是(((UsersController错误中缺少方法:控制器UsersController错误中未定义操作激活:创建UsersController::activate()文件中:app\controllers\users\u controller.))
<fieldset>
<legend>Add a article</legend>
<?php
echo $form->create('User');
echo $form->input('username');
echo $form->input('password');
echo $form->input('email');
?>
</fieldset>
<?php echo $form->end('Post project'); ?>