迁移后 Codeigniter 会话不起作用 [英] Codeigniter Sessions not working after migration
问题描述
我有一个使用 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');
但是,这不适用于跨网址.例如:
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屋!