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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab