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('wengerarsen@gmail.com', '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.
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:
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.