Codeigniter email verification pass parameters

2019-08-08 21:33发布

问题:

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.

回答1:

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.



回答2:

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);  
}