How to unserialize codeigniter session data from d

2019-05-23 14:31发布

问题:

I wan to use CI session in a external script and I got following data from database.

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

I have tried unserialize and unserialize(base64_decode($data)) but I am fail yet.

Please help to extract this data.

回答1:

I got the solution here

So I have used 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;');

So session decode stored all the encrypted data in normal php session.

Which I can access using: echo $_SESSION['ci_UserID'];

Well guys thanks for the help



回答2:

If this is a session variable, you can use CodeIgniter's own session library. Consider the following code (in a controller):

$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

Sorry, I have read "the external script" only after having posted the code. This obviously only works in the CI framework.

For an external script you may need to have a closer look. The variables are separated by ";" and "|" and then serialized, so this might work (not tested):

$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"