jQuery ajax请求创建新的Code点火器会话


jQuery ajax request creates new Code Igniter session

我正在使用Codeigniter php框架。

我的函数newRecipe()创建了一个带有验证码的表单。我的函数generate_captcha()创建一个验证码,并将其保存在用户会话中。

当用户提交表单有一些验证,然后用jQuery ajax请求发送输入的验证码到服务器,我想从客户端与用户会话中的验证码进行比较。

但是当captcha在check_captcha()中被检查时,一个新的会话被创建,我无法将来自客户端的captcha与来自旧会话的captcha进行比较。

是否有可能阻止jQuery ajax请求创建一个新的会话?

    function newRecipe(){
        $this->load->library('session');
        $this->generate_captcha();
        $data = array(
            'view' => 'newRecipe',
            'captcha' => $this->session->userdata('captcha')
        );
        $this->load->view('includes/template',$data);
    }
    function check_captcha(){
        $this->load->library('session');      
        $captcha_order = $_POST['captcha_order'];
        $order = $this->session->userdata('order'); 
        if(($captcha_order != $order)){
            $new_captcha = $this->generate_captcha();
            $comparison = false;
        }else{
            $comparison = true;
            $new_captcha = '';
        }
        echo json_encode(array('comparison'=>$comparison,'newCaptcha'=>$new_captcha));  
    }
    on client side i have jquery:
    function validate_form(){
         /*form validation*/
         captcha_pass();
    }
    function captcha_pass(){
        var captcha_order = getCaptcha();
        $('.checkCaptcha').show();
        $.post(location+'dish/check_captcha',{captcha_order: captcha_order}, function(data) { 
                        if(data.comparison == false){
                            $('.captcha_wrap').html(data.newCaptcha);
                        }else{
                           $("#new_dish_form").submit();
                        }
        },'json');
    }
 function generate_captcha(){
        $this->load->library('session');
        $this->load->library('captcha');
        if($this->session->userdata('captcha')) {
            //delete captcha images
            $this->captcha->deleteImages($this->session->userdata('order'));
        }
        $captcha = $this->captcha->generateCaptcha();
        $userCaptcha = array(
            'captcha'  => $captcha['captcha'],
            'order'     => $captcha['order'],
            'formCreate' => time()
        );
        $this->session->set_userdata($userCaptcha);
        return $captcha['captcha'];
    }

当它是ajax请求时,您可以在会话开始的地方(可能是父类)关闭新会话的创建。或者您可以将generate_captcha()放在check_captcha()之后。