How can I verify an email address with codeigniter? I just went through the manual, I couldn't find this.
With email verification, i mean the exact same verification you see when registering on a community forum.
Thanks in advance!
Use the Email Class to send the email. The email could contain a link with a "secret key", something random and hash-like, like 5dfg7898ssdf (I made that one up :) ). The link could point to: example.com/verify/user/5dfg7898ssdf Then in a codeigniter controller called "verify", you put this function (just some quick code):
function user($key = NULL)
{
if($key)
{
// Find key in database
// If it exists, then mark
// the corresponding user as "activated"
}
}
function verify($verificationText=NULL){
$noRecords = $this->HomeModel->verifyEmailAddress($verificationText);
if ($noRecords > 0){
$error = array( 'success' => "Email Verified Successfully!");
}else{
$error = array( 'error' => "Sorry Unable to Verify Your Email!");
}
$data['errormsg'] = $error;
$this->load->view('index.php', $data);
}
Related
Basically, I want send a reminder to the user to used modules in the system. If the didn't used one of the module means need remind them by sending an automated mail. I have created a DB table which contain user_id, module_id and last_used_module. So How i will send the mail to the user.
I try to used null value for last_used_module but its didn't work because last_module_used time is the same with created at and updated at. So, i cant used that.Anyone can help with this?
I got some idea to solve this issue which is if user_id and module_id exist in table means no need to the mail. I don't know hw implement this.
foreach($ModuleAutoMail as $module) {
if($mail->condition_id=='3' ){
$last_used_module = Carbon::parse($module->last_used_module);
$DeferenceInDays = Carbon::parse(Carbon::now())->diffInDays($last_used_module);
if(is_null($last_used_module)){
$ableToSendMail = true;
}
elseif ($DeferenceInDays > 7) {
$ableToSendMail = false;
}
else {
if ($emailCheck< 1){
$ableToSendMail = true;
}
}
}
}
if ($ableToSendMail) { //saving and sent email
$mails = new EmailSave;
$mails->user_id = $user->id;
$mails->email_id =$mail->id;
Mail::to($user->email)->send(new Automail($mail));
$mails->save();
}
Mail Trap
Register on Mail Trap
Copy the SMTP login password form Mail trap
Paste in ENV file
Create Mailable
Mail::send('emails.reminder', ['user' => $user], function ($m) use
($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
Visit This Tutorial
I made a function in CodeIgniter so users can log into my page. When a user enters a wrong email or password I want to echo an error message like
For example Invalid email or password. But this is not working for me. I tried to do it with flashdata like this:
else {
$this->$session->$set_flashdata("Invalid email or password");
redirect("https://localhost/8800/login.php","refresh");
}
is should be
else {
$this->session->set_flashdata('error','Invalid email or password');
redirect(base_url()); # or redirect('login'); controller name
}
and in view
if(!empty($this->session->flashdata('error'))
{
echo $this->session->flashdata('error');
}
I'm currently using codeIgniter 3. I created a registration page with username, password, password confirmation and email. For compare the two string, I used strcmp() function. Now, the problem is when I put something like
password = "moon";
confirmation_password = "moon";
It work without any problems. Logically you'll tell me :)
Otherwise, when I put something like
password = "moon";
confirmation_password = "something else";
Now it still work with show me the appropriate error message. Logically again you'll tell me. Except that another error message pop:
Unable to access an error message corresponding to your field name Password confirmation.(check_information)
I don't understand how this error message could pop only when the confirmation_password variable doesn't match with password variable.
Here's my code
function index()
{
$this->form_validation->set_rules('confirmation_password', 'Password confirmation', 'trim|required|callback_check_information');
}
function check_information($confirmation_password)
{
$password = $this->input->post('password');
if (strcmp($password, $confirmation_password))
{
echo 'password and confirmation password doesn\'t match';
return FALSE;
}
}
Does anyone can tell if is there something wrong in my code ?
I voluntarily show you a part of my code for avoid to make my post too long.
Thanks
You have to return with boolean in both cases, and not echo the message, set it:
function check_information($confirmation_password)
{
$password = $this->input->post('password');
if (strcmp($password, $confirmation_password))
{
$this->form_validation->set_message('check_information', 'password and confirmation password doesn\'t match');
return FALSE;
}
else {
return TRUE;
}
}
There is a simple solution, try this:
$this->form_validation->set_rules('confirmation_password', 'Password Confirmation', 'required|matches[password]');
i use tank auth as login handler in code igniter. when i use the forget password feature, i get a link send by mail http://xx.xx.xx/en//auth/reset_password/2/01b951fd2a02efa2d64f1fe70c2a4e3b. When i click this link it always says: "Your activation key is incorrect or expired. Please check your email again and follow the instructions."
i changed the segments so it gets the right segments but somehow it the goes wrong on if ($this->form_validation->run()). it Somehow wants the new_password and confirm_new_password as post data but from the link in the email no post data will ofcourse be sent.
Is this a bug in tank auth, is there a quickfix (does tank_auth forget a step, is something not configured right?)
reference code:
function reset_password()
{
$break =$this->uri->total_segments();
$new_pass_key= $this->uri->segment($break);
$user_id= $this->uri->segment($break-1);
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
$this->form_validation->set_rules('confirm_new_password', 'Confirm new Password', 'trim|required|xss_clean|matches[new_password]');
$data['errors'] = array();
if ($this->form_validation->run()) { //breaks here. For some reason wants to validate post data which
if (!is_null($data = $this->tank_auth->reset_password($user_id, $new_pass_key,$this->form_validation->set_value('new_password')))) { // success
$data['site_name'] = $this->config->item('website_name', 'tank_auth');
// Send email with new password
$this->_send_email('reset_password', $data['email'], $data);
$this->_show_message($this->lang->line('auth_message_new_password_activated').' '.anchor('/auth/login/', 'Login'));
} else { // fail
$this->_show_message($this->lang->line('auth_message_new_password_failed'));
}
} else {
// Try to activate user by password key (if not activated yet)
if ($this->config->item('email_activation', 'tank_auth')) {
$this->tank_auth->activate_user($user_id, $new_pass_key, FALSE);
}
if (!$this->tank_auth->can_reset_password($user_id, $new_pass_key)) {
$this->_show_message($this->lang->line('auth_message_new_password_failed'));
}
}
$this->load->view('auth/reset_password_form', $data);
}
Your new_pass_key and $user_id are wrong I guess.
It should work out of the box with this:
$user_id = $this->uri->segment(3);
$new_pass_key = $this->uri->segment(4);
EDIT:
$user_id = $this->uri->segment(4);
$new_pass_key = $this->uri->segment(5);
Why did you change that by the way?
I am trying to implement an email verification after registering an account. So after registering an account, an email will be sent to the user email to verify. the email was sent using the email class of codeigniter.
The code for sending the email is as below
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->from('admin#mathplication.com', 'admin');
$this->email->to($user_email);
$this->email->subject('Registration verification for mathplication');
$this->email->message('
Thanks for signing up!
Your account has been created, you can login with the following credentials
after you have activated your account by pressing the url below.
Please click this link to activate your account:
<a href='.base_url('verify').'?email='.$user_email.'&rand='.$user_rand.'>Click Here</a>');
$this->email->send();
and in the routes.php in the config folder
$route['verify'] = "login_register/view_verify
in which view_verify is the function in my login_register controller
inside this view_verify I will check the two parameters I passed which are the email and the random string generated.
function view_verify($email,$rand)
{
//$email = $this->input->get('email');
//$rand = $this->input->get('rand');
$this->load->database();
$this->load->model('login_model');
$result= $this->login_model->email_verification($email,$rand);
if($result==TRUE)
{
$this->load->view('pages/verify');
}
}
I will get a 404 page not found error. Not sure if my routing with the variables is the problem here or not or is there another way of passing parameters through url to the controller. Thanks in advance for the help.
If you are going to be using query strings in the url, you will need to enable that in the config. Possible there might also be issues with permitted_uri_chars because of the email address.
You could generate the url with the user id and nonce like:
<a href='.base_url('verify').$user_id.'/'.$user_rand.'>Click Here</a>');
Which should produce something like
http://www.example.com/verify/1234/23q23rq2rq24rq34rq34rq34r
Then in routes:
$route['verify/(:any)'] = "login_register/view_verify/$1";
At this point the function view_verify should work correctly exactly as it is now except you will need to adjust your model to do the lookup via user id instead of by email.
function verify($verificationText=NULL){
$noRecords = $this->HomeModel->verifyEmailAddress($verificationText);
if ($noRecords > 0){
$error = array( 'success' => "Email Verified Successfully!");
}else{
$error = array( 'error' => "Sorry Unable to Verify Your Email!");
}
$data['errormsg'] = $error;
$this->load->view('index.php', $data);
}