迁移后Codeigniter会话无法正常工作 [英] Codeigniter Sessions not working after migration

查看:71
本文介绍了迁移后Codeigniter会话无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用CodeIgniter 3.1.6构建的应用程序.我正在生产服务器上的子域上进行测试.我将主域指向该文件夹,还将config.php中的$ base_url更改为正确的URL. (从未设置config.php中的$ cookie_domain.)

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');

但是,这不适用于所有URL.例如:

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?

推荐答案

已报告了一些有关PHP 7.1和CI 3.1.6不兼容$this->session->set_userdata('name', $name);

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);

好吧,$this->session->set_userdata('name', $name);可以工作,但是函数userdata()仅接受一个参数,并希望它是一个字符串

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

如果您查看会话库(/system/libraries/Session/Session.php),则会在行

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;
}

,但是您也可以使用本机$ _SESSION来获取像$name=array('firstname'=>'mr smith')这样的数组:

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

设置:

$_SESSION['name']=$name; 

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

获取:

echo $_SESSION['name']['firstname']; //etc..

这篇关于迁移后Codeigniter会话无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆