Codeigniter Sessions not working after migration

前端 未结 2 718
盖世英雄少女心
盖世英雄少女心 2020-12-04 02:37

I have an application built using CodeIgniter 3.1.6. I was carrying out testing on a subdomain on the production server. I pointed the main domain to the folder and also cha

相关标签:
2条回答
  • 2020-12-04 03:35

    Open: system/libraries/Session/Session.php

    session_start(); to // session_start();
    

    And Change:

        ini_set('session.use_trans_sid', 0);
        ini_set('session.use_strict_mode', 1);
        ini_set('session.use_cookies', 1);
        ini_set('session.use_only_cookies', 1);
        ini_set('session.hash_function', 1);
        ini_set('session.hash_bits_per_character', 4);
    

    To

      /* ini_set('session.use_trans_sid', 0);
        ini_set('session.use_strict_mode', 1);
        ini_set('session.use_cookies', 1);
        ini_set('session.use_only_cookies', 1);
        ini_set('session.hash_function', 1);
        ini_set('session.hash_bits_per_character', 4); */
    

    Last edit open /index.php add to top:

    session_start(); 
    

    Solved.

    0 讨论(0)
  • 2020-12-04 03:39

    There have been several issues reported for incompatibility of PHP version 7.1 and CI 3.1.6 not supporting $this->session->set_userdata('name', $name);

    well, $this->session->set_userdata('name', $name); works, but the function userdata() accepts only one argument and expects it to be a string

    if you look into session library (/system/libraries/Session/Session.php), you'll find near row

    747:

    /**
     * Userdata (fetch)
     *
     * Legacy CI_Session compatibility method
     *
     * @param   string  $key    Session data key
     * @return  mixed   Session data value or NULL if not found
     */
    public function userdata($key = NULL)
    {
        if (isset($key))
        {
            return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
        }
        elseif (empty($_SESSION))
        {
            return array();
        }
    
        $userdata = array();
        $_exclude = array_merge(
            array('__ci_vars'),
            $this->get_flash_keys(),
            $this->get_temp_keys()
        );
    
        foreach (array_keys($_SESSION) as $key)
        {
            if ( ! in_array($key, $_exclude, TRUE))
            {
                $userdata[$key] = $_SESSION[$key];
            }
        }
    
        return $userdata;
    }
    

    but alternatively you can fetch an array like $name=array('firstname'=>'mr smith') with native $_SESSION like this:

    set:

    $_SESSION['name']=$name; 
    

    or

    $this->session->set_userdata('name', $name);
    

    get:

    echo $_SESSION['name']['firstname']; //etc..
    
    0 讨论(0)
提交回复
热议问题