1

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

我想在没有数据库的情况下使用验证码助手,但似乎没有任何效果。

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

   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

4 回答 4

3

像这样修改你的回调函数:

   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初始化之前。

于 2012-05-29T11:44:09.620 回答
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;
    }

}
于 2012-10-19T08:00:04.547 回答
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;
      }
   }
于 2013-05-07T13:14:37.890 回答
0

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

于 2020-03-06T03:06:10.267 回答