skip to Main Content

I have to work in multiple subdomains. Suddenly I can’t access my all subdomains. Because when the page redirects session data was lost. This issue happens my all subdomains
But I didn’t change anything from my server. Anyone, please help me why this issue suddenly happened and how to fix this issue
My domain was hosted in cpanel.
But My code working perfectly in localhost

2

Answers


  1. Chosen as BEST ANSWER

    I Changed small modification session.php file

    before change code:

    // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
            if (isset($_COOKIE[$this->_config['cookie_name']])
                && (
                    ! is_string($_COOKIE[$this->_config['cookie_name']])
                    OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])
                )
            )
            {
                unset($_COOKIE[$this->_config['cookie_name']]);
            }
    

    AFTER:

    i changed 32 instead of 40

    // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
            if (isset($_COOKIE[$this->_config['cookie_name']])
                && (
                    ! is_string($_COOKIE[$this->_config['cookie_name']])
                    OR ! preg_match('/^[0-9a-f]{32}$/', $_COOKIE[$this->_config['cookie_name']])
                )
            )
            {
                unset($_COOKIE[$this->_config['cookie_name']]);
            }
    

    In this case session data loads based on cookie id. But The cookie id was max 32.But previous code define 40.when cookie id was between 33 and 40.the session data was loss

    But My Doubt is how the previous code Worked well since 2 days before in live ?. AT the same time previous code still working in my localhost ?


  2. UPDATE: This issue gets fixed in CI v3.1.1. Please upgrade to >= 3.1.1 to totally get rid of this issue.

    @prakasht – I had been facing a similar problem and found this while looking up for a fix. For me it happened while trying to upgrade CI v2.2.6 –> v3.0.3

    Suddenly my sessions stopped working. I added some logs to debug the scenario in the sessions file-based driver system/libraries/Session/drivers/Session_files_driver.php

    Having centralized sessions in my earlier as well as current place of work, I found that somehow the session was re-created for the every request based on the logs I added.

    As a lazy person, I tried to google my way through it when I saw your answer above.

    This was till now the closest I had seen someone get to solving this issue with Ci as everyone else just adds hacks. I tried to debug the code in the file system/libraries/Session/Session.php on my own and found that the sanitization preg_match() never matches.

    In the regex trying to check the cookie value tried to match with

    if (isset($_COOKIE[$this->_config['cookie_name']])
                && (
                    ! is_string($_COOKIE[$this->_config['cookie_name']])
                    OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])
                )
            )
    

    I tried to match my cookie value with the regex ^[0-9a-f]{40}$ and found that it failed. So, going by the definition in the file application/config/config.php, I changed the regex to ^[0-9a-z_-]{1,40}$ which basically means that the regex can match any cookie value between lengths 1 – 40 comprising of characters in any one or a mix of set 0-9, a-z, _ , -.

    Code with fixed regex

    if (isset($_COOKIE[$this->_config['cookie_name']])
                && (
                    ! is_string($_COOKIE[$this->_config['cookie_name']])
                    OR ! preg_match('/^[0-9a-z_-]{1,40}$/', $_COOKIE[$this->_config['cookie_name']])
                )
            )
    

    This fixed it for me. Hope it does the same for you.

    P.S.: I have also replaced all instances of

    $this->session->all_userdata() with $this->session->userdata()

    and

    $this->session->set_userdata($key, $value) with $this->session->key = $value

    inside my codebase as per CI 2.2.x -> 3.x upgrade documentation and Session library documentation

    UPDATE: This issue gets fixed in CI v3.1.1. Please upgrade to >= 3.1.1 to totally get rid of this issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search