开发者

jQuery ajax request creates new Code Igniter session

I am using the Codeigniter php framework.

My function newRecipe() creates a form with a captcha.

My function generate_captcha() creates a captcha and saves it in the users session.

When the user submits the form there is some validation and then with a jQuery ajax request sends the captcha entered to the server where I want compare the captcha from the client with the captcha in the users session.

But when the captcha is checked in check_captcha() a new session is created and I cannot compare the captcha from the client with the captcha from the old session.

Is it possible to stop the jQuery ajax request creating a new session?

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


You can turn off the creation of new session in the place where your Session starts (maybe parent class) when it's ajax request. Or you can put your generate_captcha() after check_captcha().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜