php - jQuery ajax request creates new Code Igniter session


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

        $data = array(
            'view' => 'newRecipe',
            'captcha' => $this->session->userdata('captcha')

    function check_captcha(){
        $captcha_order = $_POST['captcha_order'];
        $order = $this->session->userdata('order'); 

        if(($captcha_order != $order)){
            $new_captcha = $this->generate_captcha();
            $comparison = false;
            $comparison = true;
            $new_captcha = '';
        echo json_encode(array('comparison'=>$comparison,'newCaptcha'=>$new_captcha));  

    on client side i have jquery:

    function validate_form(){
         /*form validation*/

    function captcha_pass(){
        var captcha_order = getCaptcha();
        $.post(location+'dish/check_captcha',{captcha_order: captcha_order}, function(data) { 
                        if(data.comparison == false){

 function generate_captcha(){
        if($this->session->userdata('captcha')) {
            //delete captcha images

        $captcha = $this->captcha->generateCaptcha();
        $userCaptcha = array(
            'captcha'  => $captcha['captcha'],
            'order'     => $captcha['order'],
            'formCreate' => time()
        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().

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


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.