Codeigniter中的怪异会话行为 [英] Weird session behaviour in codeigniter
问题描述
以下代码工作正常..
Following code works fine ..
$somearray = getData();
$data = array(
'user_display_name' => $userdisplayname,
'username' => $usernamefromdb,
'logged_in' => TRUE,
);
$this->session->set_userdata($data); // used to create user session
此功能可与Codeigniter和sqlite一起使用。.
This works fine with codeigniter and sqlite..
但是当我编码
$data = array(
'user_display_name' => $userdisplayname,
'username' => $usernamefromdb,
'logged_in' => TRUE,
'arrdata' => $somearray
);
$this->session->set_userdata($data); // used to create user session
它表示会话已终止...可能是什么问题?
It says session terminated... What can be the issue?
当我执行 var_dump($ somearray)
时,它会显示信息。会话是否有内存限制?
When i do var_dump($somearray)
it shows the info. Is there any memory limit for sessions??
谢谢
推荐答案
我在我的一个应用程序中也注意到了相同的问题。调试问题使我发现CodeIgniter没有正确实现多维数组的序列化/反序列化。实际上有一个为此提交的错误,我相信他们要么已将其修复,要么将要修复。看看他们在Core中的会话库。
I noticed the same issue in one of my applications too. Debugging the problem lead me to discover that CodeIgniter is (was) not properly implementing serialization/unserialization of multi-dimensional arrays. There was actually a bug submitted for that, and I believe they either fixed it or about to fix it. Take a look at the their session lib in Core.
function _serialize($data)
{
if (is_array($data))
{
foreach ($data as $key => $val)
{
if (is_string($val))
{
$data[$key] = str_replace('\\', '{{slash}}', $val);
}
}
}
else
{
if (is_string($data))
{
$data = str_replace('\\', '{{slash}}', $data);
}
}
return serialize($data);
}
请注意,它仅通过数组的1个级别,并且仅通过级别可以替换斜线。问题是您拥有多维数组,并且那里可能存在数据,这些数据无法进行序列化并清除会话。我们通过扩展其会话库并创建以下调用来解决此问题:
Notice that it only goes through 1 level of your array and that the ONLY level that gets the slashes replaced. The problem is that you have multi-dimensional array and there's probably data there that's throwing off serialization and wiping your session. We fixed it by extending their session library and creating these calls:
class MY_Session extends CI_Session {
public function __construct()
{
parent::__construct();
}
function _serialize($data)
{
$data = $this->_serialize_backslash_recursive($data);
return serialize($data);
}
function _unserialize($data)
{
$data = @unserialize(strip_slashes($data));
return $this->_unserialize_backslash_recursive($data);
}
function _serialize_backslash_recursive($data)
{
if (is_array($data))
{
return array_map(array($this,'_serialize_backslash_recursive'), $data);
}
else
{
if (is_string($data))
{
return str_replace('\\', '{{slash}}', $data);
}
}
return $data;
}
function _unserialize_backslash_recursive($data)
{
if (is_array($data))
{
return array_map(array($this,'_unserialize_backslash_recursive'), $data);
}
else
{
if (is_string($data))
{
return str_replace('{{slash}}', '\\', $data);
}
}
return $data;
}
}
现在正确地通过所有级别。试试看,看看它是否适合您。
This will now go through all the levels properly. Give it a try and see if it works for you.
这篇关于Codeigniter中的怪异会话行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!