codeigniter电子邮件验证

时间:2017-06-25 12:31:23

标签: php codeigniter email-validation

大家好,我正在尝试向注册用户发送电子邮件验证短信。邮件已发送,确认工作但状态为'单击认证后,数据库中的行不会更新为1.

这是我的代码CONTROLLER

    public function verify($email_code, $email_address) {
    if($this->user_model->verifyEmail($email_code, $email_address)){
    $this->load->view('templates/header');
    $this->load->view('users/email_validated');
    $this->load->view('templates/footer'); 
    } else {
        echo 'error'.$this->config->item('admin_email');    
    }

}

这是我的代码MODEL

public function send_validation_email() {
    $username = $this->input->post('username');
    $email = $this->input->post('email');

    $sql = "SELECT id, register_date FROM users WHERE email = '" .$email ."' LIMIT 1";
    $result = $this->db->query($sql);
    $row = $result->row();

    $this->email_code = md5((string)$row->register_date);
    $email_code = $this->email_code;

    $this->email->set_mailtype('html');
    $this->email->from($this->config->item('bot_email'),'talkativs');
    $this->email->to('xxxxx@gmail.com');
    $this->email->subject('activatorrrrr');

    $message .= '<p> Dear ' . $username.',</p>';

    $message .= '<p> confim mail sharpaly <a href="' . base_url().'users/verify/'.$email.'/'.$email_code.'">click here</a></p>';
    $message .= '<p> Thanks</p>';

    $this->email->message($message);
    $this->email->send();

    if ($this->email->send()){
        return "sent";
    } else {
        return "failed to send";    
    }

}


 function verifyEmail($email_code, $email_address) {
    $data = array('status' => 1);
    $this->db->where('email', $email_address);
    $this->db->where('md5(register_date)', $email_code);
    return $this->db->update('users', $data);
}

1 个答案:

答案 0 :(得分:1)

您的方法参数映射错误。

public function verify($email_code, $email_address) {
        if($this->user_model->verifyEmail($email_code, $email_address)){
        $this->load->view('templates/header');
        $this->load->view('users/email_validated');
        $this->load->view('templates/footer'); 
        } else {
            echo 'error'.$this->config->item('admin_email');    
        }
    }

必须如下:

public function verify($email_address, $email_code) {
    if($this->user_model->verifyEmail($email_code, $email_address)){
    $this->load->view('templates/header');
    $this->load->view('users/email_validated');
    $this->load->view('templates/footer'); 
    } else {
        echo 'error'.$this->config->item('admin_email');    
    }
}

您发送的电子邮件如下所示:

'<p> confirm mail sharply <a href="' . base_url().'users/verify/'.$email.'/'.$email_code.'">click here</a></p>';

在电子邮件中产生以下输出:

<p>confirm mail sharply <a href="http:// www.site.com/users/verify/example@email.com/AC67">click here</a></p>

请注意链接的格式:

  

用户{的控制器} / {验证的方法} / {example@email.com的第一    Param } / AC67 {第二次参数}

所以你的方法必须使用 方法($ email,$ code) ......简单......:)

因为CodeIgniter以与提供的顺序相同的顺序获取url参数。