激活用户帐户 - 电子邮件

时间:2016-11-01 08:34:02

标签: php codeigniter

我正在尝试创建一个用户注册表&通过电子邮件激活帐户。电子邮件成功发送,链接确认成功点击,并按照我的设置说“激活成功”。但数据库中的“状态”不会改变。有人能帮我吗 ?这段代码有什么问题。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Voter_model extends CI_Model
{


    function __construct()
    {
         // Call the Model constructor
         parent::__construct();
    }

    //get the username & password from tbl_usrs
    function get_user($username, $password)
    {
         $sql = "select * from user where username = '" . $username . "' and password = '" . $password . "' and status = '1'";
         $query = $this->db->query($sql);
         return $query->num_rows();
    }


     //insert into user table
   function insertUser($data)
   {
       return $this->db->insert('user', $data);
   }

   //send verification email to user's email id
   function sendEmail($to_email)
   {
       $from_email = 'evotingcucukan@gmail.com'; //change this to yours
       $subject = 'Aktivasi Akun';
       $message = 'Hallo'. $username .'<br /><br />Silakan klik link dibawah ini untuk mengkonfirmasi akun Anda pada Sistem E-voting Desa Pakraman Cucukan.<br /><br /> 
 '. site_url('voter/voter_register/verify/'.md5($to_email)) . '<br /><br /><br />Terima Kasih<br />Admin Evoting';

       //configure email settings
       $config['protocol'] = 'smtp';
       $config['smtp_host'] = 'ssl://smtp.googlemail.com'; //smtp host name
       $config['smtp_port'] = '465'; //smtp port number
       $config['smtp_user'] = $from_email;
       $config['smtp_pass'] = 'evoting2016'; //$from_email password
       $config['mailtype'] = 'html';
       $config['charset'] = 'iso-8859-1';
       $config['wordwrap'] = TRUE;
       $config['newline'] = "\r\n"; //use double quotes
       $this->email->initialize($config);

       //send mail
       $this->email->from($from_email, 'Admin Evoting');
       $this->email->to($to_email);
       $this->email->subject($subject);
       $this->email->message($message);
       return $this->email->send();
   }

   //activate user account
   function verifyEmailID($key)
    {
   $data = array('status' => 1);
   $this->db->where('email', $key);
   $verify=$this->db->update('user', $data);
    if($verify==true) 
         {
          return true;
        }
          return false;
    }



}?>

和我的控制器是,

<?php
class Voter_register extends CI_Controller
{
   public function __construct()
   {
       parent::__construct();
       $this->load->helper(array('form','url'));
       $this->load->library(array('session', 'form_validation', 'email'));
       $this->load->database();
       $this->load->model('Voter_model');
   }

   function index()
   {
       $this->register();
   }



   function register()
   {
       //set validation rules

       $this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[user.email]');
       $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]');
       $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');

       //validate form input
       if ($this->form_validation->run() == FALSE)
       {
           // fails
           $this->load->view('voter/voter_register');
       }
       else
       {
           //insert the user registration details into database
           $data = array(
               'fname' => $this->input->post('fname'),
               'lname' => $this->input->post('lname'),
               'email' => $this->input->post('email'),
               'password' => $this->input->post('password'),
                 'username' => $this->input->post('username'),
                 'voting'=> 1


           );

           // insert form data into database
           if ($this->Voter_model->insertUser($data))
           {
               // send email
               if ($this->Voter_model->sendEmail($this->input->post('email')))
               {
                   // successfully sent mail
                   $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Registrasi Sukses, silakan cek e-mail Anda untuk melakukan konfirmasi dan aktivasi akun.</div>');
                   redirect('admin_voter');
               }
               else
               {
                   // error
                   $this->session->set_flashdata('msg','<div class="alert alert-info text-center">Registrasi Sukses, gagal mengirim e-mail verifikasi.</div>');
                   redirect('admin_voter');
               }
           }
           else
           {
               // error
               $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
               redirect('admin_voter');
           }
       }
   }

   function verify($hash)
   {
        $this->load->helper('url');
        $this->load->model('voter_model');

       if ($this->voter_model->verifyEmailID($hash))
       {

          redirect('voter/voter_aktivasiok');
       }
       else
       {

           redirect('voter/voter_aktivasifail');
       }
   }
}
?>

感谢您提供任何帮助。

3 个答案:

答案 0 :(得分:0)

您执行加密MD5

md5($to_email))

同时添加urlencode()

你不解密MD5 ..

function verify($hash)
   {

        $hash =  base64_decode(urldecode($hash));

        $this->load->helper('url');
        $this->load->model('voter_model');

       if ($this->voter_model->verifyEmailID($hash))
       {

          redirect('voter/voter_aktivasiok');
       }
       else
       {

           redirect('voter/voter_aktivasifail');
       }
   }

答案 1 :(得分:0)

错误号码:1267

操作'='的非法混合排序(latin1_swedish_ci,IMPLICIT)和(utf8_general_ci,COERCIBLE)

UPDATE user SET status = 1 WHERE email =' ޞw }ѭ= ޻i : ^ '

文件名:C:/xampp/htdocs/evotingcucukan/application/models/Voter_model.php

行号:61

答案 2 :(得分:0)

更新模型&#34; activationAccount&#34;具有以下代码的函数: -
   &lt;?php
  / *激活用户帐户* /
   function verifyEmailID($ key)
    {
   $ data = array(&#39; status&#39; =&gt; 1);
   $ this-&gt; db-&gt; where(&#39; MD5(email)&#39;,$ key,FALSE);
   $ verify = $ this-&gt; db-&gt; update(&#39; user&#39;,$ data);
    if($ verify == true)
         {
          返回true;
        }
          返回false;
    }
  }
?&GT;