Codeigniter验证码

uttx8gqw  于 2023-03-16  发布在  其他
关注(0)|答案(4)|浏览(167)

我遇到了一个小问题,我想不通。
我想使用验证码助手没有数据库,但似乎没有工作。
在构造中,我创建了一个生成随机字符串的变量

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

什么都不起作用,问题是,验证码是与图像一起显示的,问题是验证,如果我在验证码字段中键入正确的数字,它总是显示错误,无论我键入什么,请有人给予我一个提示?
网页:

<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');
rqqzpn5f

rqqzpn5f1#

按如下方式修改回调函数:

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);这一行移出构造函数,因为每次加载控制器时,它都会生成一个新的字符串,导致验证码初始加载和验证码验证的值不同。

63lcw9qa

63lcw9qa2#

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

}
kgsdhlau

kgsdhlau3#

$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;
      }
   }
zpjtge22

zpjtge224#

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

相关问题