php - jQuery ajax request creates new Code Igniter session

194

I am using the Codeigniter php framework.

My functionnewRecipe() creates a form with a captcha.
My functiongenerate_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 incheck_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'];
    }
293

Answer

Solution:

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().

People are also looking for solutions to the problem: One request at a time with PHP and MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.