如何从数据库中反序列化codeigniter会话数据

时间:2015-11-03 08:05:03

标签: php codeigniter

我想在外部脚本中使用CI会话,并从数据库中获取以下数据。

 __ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example@xyz.com";logged_in|b:1;

我已尝试过unserializeunserialize(base64_decode($data)),但我还是失败了。

请帮助提取此数据。

2 个答案:

答案 0 :(得分:2)

我得到了解决方案here

所以我使用了session decode

session_decode('__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example@xyz.com";logged_in|b:1;');

因此会话解码在正常的php会话中存储了所有加密数据。

我可以使用echo $_SESSION['ci_UserID'];

访问

好的家伙感谢您的帮助

答案 1 :(得分:0)

如果这是一个会话变量,您可以使用CodeIgniter自己的会话库。请考虑以下代码(在控制器中):

$this->load->library('session');                // load the session library
$session_data = $this->session->all_userdata(); // get all session data
print_r($session_data);                         // print and get the corrresponding variable name, e.g. "item"
$var = $this->session->userdata('item');        // pick one that suits your needs, e.g. item

抱歉,我在发布代码后才阅读“外部脚本”。这显然只适用于CI框架。

对于外部脚本,您可能需要仔细查看。变量用“;”分隔和“|”然后序列化,所以这可能有效(未经测试):

$row = explode(';', '__ci_last_regenerate|i:1446535049;ci_UserID|s:1:"2";ci_UserName|s:24:"example@xyz.com";logged_in|b:1;'); // load the database row
$userid = explode('|', $row[1]);
$userid = unserialize($userid[1]); // now $userid holds the value "2"