注册电子邮件激活确认问题。即使串联,电子邮件 URL 中仍缺少激活码字符串

发布于 2024-10-09 17:00:33 字数 2292 浏览 0 评论 0原文

我有这个方法:

public function activation_code() 
{ //activation code sent into db
  $activation_code = random_string('alnum', 32);
  return $activation_code;
}  

我想做的就是提供发送到我的数据库的发布数据,同时提供相同激活代码的副本,以便我可以将其与“单击此处确认电子邮件”网址连接起来这是我在注册时发送给用户的确认电子邮件中的内容。

我该怎么做?我无法提供该方法,因为如果我执行数据库代码和电子邮件 URL 代码将会不同,因此用户将无法匹配它们并确认他们的电子邮件地址。

我尝试了许多其他方法,例如在一个地方提供该方法,例如,

public function create() 
{ //get post data and insert into db

  $dbcolumn->group_id = 2; //group 1 for admin group 2 for member
  $dbcolumn->first_name = $this->input->post('first_name');
  $dbcolumn->last_name = $this->input->post('last_name');
  $dbcolumn->email = $this->input->post('email');
  $dbcolumn->password = $this->hashed();
  $dbcolumn->birthday = $this->input->post('year') . 
    '-' . $this->input->post('month') . '-' . $this->input->post('day');
  $dbcolumn->sex = $this->input->post('sex');       
  $dbcolumn->activation_code = $this->activation_code();
  // date and time user joined the website
  $dbcolumn->created_on = date('Y-m-d H:i:s', now()); 
  $this->db->insert('users', $dbcolumn);
}

如果您查看 dbcolumn-> 激活代码行,您就会看到我所做的事情。这样就可以了,代码存储在数据库中。如果我向发送的电子邮件提供相同的“$this->activation_code() 方法,则代码显然会有所不同。

public function send_confirmation_email() 
{ //receives variable from create method
  $this->load->library('email');
  $this->email->from('[email protected]', 'my site');
  $this->email->to($this->input->post('email'));
  $this->email->subject('my site - Activate your account');
  //copy of activation code returned from create method
  $this->email->message('We\'re back, please click the link to activate your account ' . anchor('http://mysite.com/activation/' . $this->activation_code(), 'Activate my account'));
  $this->email->send();
}

如您所见,我将相同的方法 $activation_code() 拉入我的发送确认电子邮件方法中。这将只是生成一个全新的代码,这意味着我无法匹配数据库激活代码和用户电子邮件中的 URI 段代码

我尝试将返回中的变量公开并在发送确认电子邮件方法中调用它,但是它不起作用。

我尝试了很多不同的方法,但没有成功。

建议

、示例等将不胜感激。

I have this method:

public function activation_code() 
{ //activation code sent into db
  $activation_code = random_string('alnum', 32);
  return $activation_code;
}  

What I'm trying to do is provide this with the post data that gets sent to my database but also provide a copy of that same activation code so I can concatenate it with the "click here to confirm email" url that is in my confirmation email that is sent to users upon registration.

How can I do this? I can't provide the method because if I do the database code and the email URL code will be different so user wouldn't be able to match them and confirm their email address.

I've tried many other ways such as providing the method in one place e.g.

public function create() 
{ //get post data and insert into db

  $dbcolumn->group_id = 2; //group 1 for admin group 2 for member
  $dbcolumn->first_name = $this->input->post('first_name');
  $dbcolumn->last_name = $this->input->post('last_name');
  $dbcolumn->email = $this->input->post('email');
  $dbcolumn->password = $this->hashed();
  $dbcolumn->birthday = $this->input->post('year') . 
    '-' . $this->input->post('month') . '-' . $this->input->post('day');
  $dbcolumn->sex = $this->input->post('sex');       
  $dbcolumn->activation_code = $this->activation_code();
  // date and time user joined the website
  $dbcolumn->created_on = date('Y-m-d H:i:s', now()); 
  $this->db->insert('users', $dbcolumn);
}

If you look at the dbcolumn->activation code line you'll see what I've done. That works and the code is stored in the database. If I provide the same "$this->activation_code() method to the email that's sent the codes will obviously be different.

public function send_confirmation_email() 
{ //receives variable from create method
  $this->load->library('email');
  $this->email->from('[email protected]', 'my site');
  $this->email->to($this->input->post('email'));
  $this->email->subject('my site - Activate your account');
  //copy of activation code returned from create method
  $this->email->message('We\'re back, please click the link to activate your account ' . anchor('http://mysite.com/activation/' . $this->activation_code(), 'Activate my account'));
  $this->email->send();
}

As you can see I have the same method $activation_code() pulled into my send confirmation email method. This will just generate a whole new code meaning I won't be able to match the database activation code and the URI segment code in the users email.

I have tried to make the variable in the return public and call it in the send confirmaton email method but it doesn't work. The code ends up missing from he end of the URL in the email.

I've tried so many different ways and nothings working.

Maybe I'm missing something here?

Advice, examples etc will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-10-16 17:00:33

每次调用 activation_code() 时,您都会创建一个新代码,因为您仅将其存储在该函数的范围内。

更好的想法是将其存储为对象属性,如下所示:

public var $_activation_code = null;

public function activation_code() {
    if (is_null($this->_activation_code)) {
        $this->_activation_code = random_string('alnum', 32);
    }
    return $this->_activation_code;
}

如果尚未为此对象执行此操作,则这将创建代码,或者如果该方法已被多次调用,则仅返回当前代码,这意味着代码在整个对象中是一致的。

Every time you're calling activation_code() you're going to be creating a new code, because you're only storing it in the scope of that function.

A better idea would be to store it as an object property, like follows:

public var $_activation_code = null;

public function activation_code() {
    if (is_null($this->_activation_code)) {
        $this->_activation_code = random_string('alnum', 32);
    }
    return $this->_activation_code;
}

This will create the code if it hasn't already been done so for this object, or will simply return the current code if the method has been called more than once, meaning the code will be consistent across the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文