Solved

unserializing session data without creating new session

Posted on 2004-10-27
262 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
Question by:drew22
    5 Comments
     
    LVL 48

    Accepted Solution

    by:
    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
    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
    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
    I'll try it again.  Did you try it with session data containing arrays?
    0
     
    LVL 48

    Expert Comment

    by:hernst42
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now