Codeigniter验证码

时间:2012-05-29 11:35:18

标签: codeigniter captcha

我遇到了一个我无法弄清楚的小问题。

我希望在没有数据库的情况下使用验证码助手,但似乎没有任何工作。

在构造中我创建了一个生成随机字符串的变量

   public function __construct()
   {
      parent::__construct();
      $this->rand = random_string('numeric', 4);
   }

将此变量传递给验证码值,如下所示:

 $vals = array(
     'word'   => $this->rand,
     'img_path'  => './captcha/',
     'img_url'   => base_url() . 'captcha/',
     'font_path'    => './system/fonts/texb.ttf',
     'img_width'    => '150',
     'img_height' => 30,
     'expiration' => 7200
     );

 $cap = create_captcha($vals);

并希望使用像这样的回调函数验证它

   function captcha_check()
   {
      if($this->input->post('code') != $this->rand)
      {
         $this->form_validation->set_message('captcha_check', 'Wrong captcha code, hmm are you the Terminator?');
         return false;
      }
   }

没有任何作用,问题是,验证码代码与图像一起显示,问题是验证,如果我在验证码字段中输入正确的数字它总是向我显示错误,没有我输入的内容,可以请别人给我一个暗示吗?

HTML:

  <label for="code">Count please </label>
      <?php echo $rand; ?>
  <input type="text" id="code" name="code" class="span3" />

验证线:

$this->form_validation->set_rules('code', 'Captcha', 'required|callback_captcha_check');

4 个答案:

答案 0 :(得分:2)

修改你的回调函数:

   function captcha_check()
   {
      if($this->input->post('code') != $this->rand)
      {
         $this->form_validation->set_message('captcha_check', 'Wrong captcha code, hmm are you the Terminator?');
         return false;
      }
      return true;
   }

编辑:

您需要将此行$this->rand = random_string('numeric', 4);移出构造函数,因为在控制器的每次加载时它都会生成一个新字符串,从而导致验证码初始加载和验证码验证的值不同。只需将其放在$vals初始化之前。

答案 1 :(得分:1)

 * Example of captcha validation without database
 * Instead of it used session to store captcha value
 * The images will be deleted after the use

-

public function index()
    {   
        $this->load->helper(array('form', 'url','captcha'));
        $this->load->library('form_validation');

       $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
       $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
       $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
       $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha');

    if($this->form_validation->run() == FALSE)
       {

        $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
        $original_string = implode("", $original_string);
        $captcha = substr(str_shuffle($original_string), 0, 6);

         //Field validation failed.  User redirected to login page
        $vals = array(
                'word' => $captcha,
                'img_path' => './captcha/',
                'img_url' => 'http://mycodeignitor.org/captcha/',
                'font_path' => BASEPATH.'fonts/texb.ttf',
                'img_width' => 150,
                'img_height' => 50,
                'expiration' => 7200
        );

        $cap = create_captcha($vals);
        $data['image'] = $cap['image'];

        if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image']))
            unlink(BASEPATH."../captcha/".$this->session->userdata['image']);

        $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg'));
        $this->load->view('index_index',$data);
       }
       else
       {
            if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image']))
                unlink(BASEPATH."../captcha/".$this->session->userdata['image']);

            $this->session->unset_userdata('captcha');
            $this->session->unset_userdata('image');
            redirect('home', 'refresh');
       }



}

public function validate_captcha(){
    if($this->input->post('captcha') != $this->session->userdata['captcha'])
    {
        $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?');
        return false;
    }else{
        return true;
    }

}

答案 2 :(得分:0)

$vals = array(
     'word'   => $this->rand,
     'img_path'  => './captcha/',
     'img_url'   => base_url() . 'captcha/',
     'font_path'    => './system/fonts/texb.ttf',
     'img_width'    => '150',
     'img_height' => 30,
     'expiration' => 7200
     );

$cap = create_captcha($vals);
$this->session->set_userdata($cap);

 function captcha_check()
   {
      if($this->input->post('code') == $this->session->userdata('word'))
      {
return true;
}else{
         $this->form_validation->set_message('captcha_check', 'Wrong captcha code, hmm are you the Terminator?');
         return false;
      }
   }

答案 3 :(得分:0)

尝试使用验证码服务(例如MTCaptcha),其中不需要设置数据库。验证码可以在后端直接验证(在本例中为codeigniter)。