Code Ignitor Session and page loading issue

Viewed 89

I have one issue with the codeignitor sessions. I create one controller called "Welcome.php" in that I have three functions. one is index, second one is home and last one is getAjaxTestData.

When I load welcome controller in browser (http://localhost:9019/welcome) it will call index function by default and sets some session and it loads the welcome view. In the view file I am sending the ajax request to getAjaxTestData function and in that function I set sleep 50 seconds.

Once ajax request is called, it waits for the 50 seconds. In the mean time if I open another link by calling Home function link "https://localhost:9019/welcome/home" it is not loading till the previous ajax request is completed.

This is happening only when I set sessions. If I do not set sessions, then even if I set sleep in getAjaxTestData function, then https://localhost:9019/welcome/home will open immediately.

below is my controller- can you please check ..why the other pages are waiting to load till the previous page ajax request is completed when use sessions? is there any thing wrong?

Code in my Welcome.php controller

public function index()
    {
            
            $newdata = array(
                    'user_id' => "123",
                    'username' => "siddu",
             );
            
            $this->session->set_userdata($newdata);
            session_write_close();
            $this->load->view('welcome_message');
    }
        public function getAjaxTestData()
        {
            //echo "<pre>";print_r($this->session->userdata);echo "</pre>";die();
            sleep(15);
            $data=array();
            $data['success']=0;
            $data['start_time']=date("Y-m-d h:m:s");
            
            $data['end_time']=date("Y-m-d h:m:s");
            $data['success']=1;
            echo json_encode($data);
            die();
        }
        function Home()
        {
            $this->load->view('home');
        }
2 Answers

There's nothing wrong in your code. CodeIgniter re-write Session library to add lock by default to prevent two HTTP requests using the same session could run exactly at the same time. Check out codeigniter document here

@Canh's answer is correct.

I experienced the same issue recently with my CI3 and fixed it by extending the session library and used session_write_close, as recommended in docs, to unlock the session on all methods that write to the session, that I could find:

// Relative path: application/libraries/Session/MY_Session.php
class MY_Session extends CI_Session {
    public function __construct(array $params = array())
    {
        parent::__construct($params);
        session_write_close();
    }

    public function mark_as_flash($key)
    {
        @session_start();
        parent::mark_as_flash($key);
        session_write_close();
    }

    public function unmark_flash($key)
    {
        @session_start();
        parent::unmark_flash($key);
        session_write_close();
    }

    public function mark_as_temp($key, $ttl = 300)
    {
        @session_start();
        parent::mark_as_temp($key, $ttl);
        session_write_close();
    }

    public function unmark_temp($key)
    {
        @session_start();
        parent::unmark_temp($key);
        session_write_close();
    }

    public function __set($key, $value)
    {
        @session_start();
        parent::__set($key, $value);
        session_write_close();
    }

    public function set_userdata($data, $value = NULL)
    {
        @session_start();
        parent::set_userdata($data, $value);
        session_write_close();
    }

    public function unset_userdata($key)
    {
        @session_start();
        parent::unset_userdata($key);
        session_write_close();
    }

    public function set_tempdata($data, $value = NULL, $ttl = 300)
    {
        @session_start();
        parent::set_tempdata($data, $value, $ttl);
        session_write_close();
    }
    public function unset_tempdata($key)
    {
        @session_start();
        parent::unset_tempdata($key);
        session_write_close();
    }
}
Related