Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

php - Codeigniter Sessions not working after migration

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 changed $base_url in config.php to the correct URL. ($cookie_domain within config.php has never been set.)

However, session data is now not working. I have tried some testing, the session data can be set and read within one controller.

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

However this doesn't work across URLs. For example:

// controllers/Contact.php
$this->session->set_userdata('name', $name); 

// controllers/Welcome.php
echo $this->session->userdata('name');

Any ideas as to why this may not work on a different domain?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...