验证captcha码点火器


validating the captcha codeigniter

嘿,伙计们,我正在尝试验证codeigniter中的captcha代码。有没有一种简单的方法来验证captcha?这是我的代码,它不起作用,因为每次加载页面时,它都会调用一个新的captcha来替换会话中的单词。

public function index() {
    $originalString = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
    $originalString = implode("", $originalString);
    $captcha = substr(str_shuffle($originalString), 0, 6);
    $vals = array(
            'word' => $captcha,
            'img_path' => './captcha/',
            'img_url' =>  base_url().'captcha/',
            'font_path' => './system/fonts/texb.ttf',
            'img_width' => 150,
            'img_height' => 50,
            'expiration' => 7200 );
    $cap = create_captcha($vals);
    $captchaImage = $cap['image'];
    $this->session->set_userdata($cap);
    if(isset($_POST['register'])) {
        $this->form_validation->set_rules('firstName', 'First Name', 'required');
        $this->form_validation->set_rules('lastName', 'Last Name', 'required');
        $this->form_validation->set_rules('emailAddress', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[6]');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[confirm-password]|min_length[6]');
        $this->form_validation->set_rules('confirm-password', 'Password Confirm', 'required');
        $this->form_validation->set_rules('secretquestion', 'Secret Question', 'required');
        $this->form_validation->set_rules('answer', 'Answer', 'required');
        $this->form_validation->set_rules('inputCode', 'Captcha', 'required|');
         if ($this->form_validation->run() == TRUE) {
            $user = $this->Account_Model->validation($_POST['username']);
            if($_POST['inputCode'] != $this->session->userdata('word')){
                echo $_POST['inputCode'] .' = ' .$this->session->userdata('word');
                $this->_error = 'Code does not match the image';
            } else {
                if(empty($user)) {
                    $this->load->library('upload');
                    $accountId = $this->Account_Model->addUser($_POST);
                    $config['upload_path'] = './assets/uploads/avatars';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size']    = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    $config['file_name'] = $_POST['username'];
                    $this->upload->initialize($config);
                    $this->load->library('upload', $config);
                    if ( !$this->upload->do_upload('avatar'))
                    {
                        $error = array('error' => $this->upload->display_errors());
                    }
                    if($accountId){
                        $this->_body = array(
                            'username' => $_POST['username'],
                            'email' => $_POST['emailAddress'],
                            'secretQuestion' => $_POST['secretquestion'],
                            'answer' => $_POST['answer'],
                            'captchaImage' => $captchaImage
                        );
                         $this->_template = 'register_success';
                        return $this->_renderPage();
                    }
                }
            }
         }
    }
    $this->_body = array(
        'secretQuestions' => $this->questions,
        'captchaImage' => $captchaImage,
        'error' => $this->_error
    );
    return $this->_renderPage();
}

有更好的方法吗??。。请帮忙。。tnx处于高级状态。。

也许最简单的方法是在用户提交表单后不创建新的captcha,或者至少不更新会话。

if(!$_POST){
 $this->session->set_userdata($cap);
}