dszdiavv474681 2013-11-25 00:43 采纳率: 0%
浏览 49
已采纳

验证验证码codeigniter

hey guys im trying to validate a captcha code in codeigniter. is there an easy way to validate the captcha? here is my code and it is not working because everytime it loads the page it will call a new captcha which will replace the word in the session.

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

is there a better way to do this??.. please help.. tnx in advanced..

  • 写回答

1条回答 默认 最新

  • dongqu9917 2013-11-25 01:18
    关注

    Maybe the easiest way would be to not create a new captcha once the user submits the form, or at least not update the session.

    if(!$_POST){
     $this->session->set_userdata($cap);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题