Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

unserializing session data without creating new session

Posted on 2004-10-27
7
Medium Priority
?
268 Views
Last Modified: 2006-11-17
I'm trying to create a session browser that will allow me to inspect sessions in my session table.
I found this function somewhere to convert  session data to an array, but it only works with variables in the session, not arrays in the session

function sess_string_to_array($sd)
{
      $sess_array = array();
      $vars = preg_split('/[;}]/', $sd);

      for ($i=0; $i < sizeof($vars); $i++)
      {
            $parts = explode('|', $vars[$i]);
            $key = $parts[0];
            $val = unserialize($parts[1].";");

            $sess_array[$key] = $val;
      }
      
      return $sess_array;
}
0
Comment
Question by:drew22
  • 3
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 12423555
Try this one:
function unserializesession($data) {
   $vars=preg_split('/([a-z,A-Z]+)\|/',$data,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
   for($i=0; $vars[$i]; $i++) {
       $result[$vars[$i++]]=unserialize($vars[$i]);
   }
   return $result;
}

Taken from http://de3.php.net/manual/en/function.session-decode.php#46114
0
 
LVL 1

Author Comment

by:drew22
ID: 12424458
hernst42:

Didn't work for me.  
I might try http://www.evilwalrus.com/viewcode.php?codeEx=553 , but it is huge, and I can always manually extract arrays via this hard-coded:

      $ar_name = 'completed|'

       $p1 = strpos($sess_data, $ar_name');
      $p2 = strpos($sess_data, ';}',$p1);
      $s= substr($sess_data,$p1+strlen($ar_name),$p2-$p1+2);  
      
        $completed = unserialize($s);
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12431107
are you using windows? On my linux-box where I tried it, it worked perfect (including an object)
0
 
LVL 1

Author Comment

by:drew22
ID: 12436138
I'll try it again.  Did you try it with session data containing arrays?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12436507
Yes, I tried it with an objects that contained an array and additional array:
e.g.

sessionreg|s:7:"Orc_RIP";SESSIONID|s:15:"1098978095.8022";RIP|a:3:{s:10:"inputmulti";a:1:{s:4:"link";a:1:{s:6:"params";s:90:"./inputdatamulti.php?cmd=edit&class=Collection&SESSIONID=1098978095.8022&Collection=100109";}}s:6:"brunch";a:2:{s:7:"showall";a:1:{s:6:"PathID";a:2:{i:0;s:8:"03010103";i:1;s:10:"0301010301";}}s:10:"showbranch";a:1:{s:6:"PathID";a:4:{i:0;s:8:"03010103";i:1;s:6:"030101";i:2;s:4:"0301";i:3;s:10:"0301010301";}}}s:9:"DataPoint";a:3:{i:67;a:20:....

var_dump(of that function returned:
array(4) {
  ["sessionreg"]=>
  string(7) "Orc_RIP"
  ["SESSIONID"]=>
  string(15) "1098978095.8022"
  ["RIP"]=>
  array(3) {
    ["inputmulti"]=>
    array(1) {
      ["link"]=>
      array(1) {
        ["params"]=>
        string(90) "./inputdatamulti.php?cmd=edit&class=Collection&SESSIONID=1098978095.8022&Collection=100109"
      }
    }
    ["brunch"]=>
    array(2) {
      ["showall"]=>
      array(1) {
        ["PathID"]=>
        array(2) {
          [0]=>
          string(8) "03010103"
          [1]=>
          string(10) "0301010301"
        }
      }
.....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question