SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Custom Session Handler

    sorry, this is brief, but I hope someone finds it useful. I sure would have.. (-;

    S

    ----
    http://www.php.net/manual/en/functio...ve-handler.php

    sean@php.net
    31-Jul-2002 12:19

    I wrote this because I needed the actual variables being written with a
    session write...

    The data that is sent to sess_write is a strange mix of keys and
    serialized data. Here's a function to unserialize that data:

    PHP Code:
    function unserialize_session($val)
    {

        
    // not entirely foolproof, but replace the pipe with something obscure
        
    $replacer chr(254).chr(254).chr(254);
        
    $repRegex '/(")([A-Za-z0-9_]*?)(\|)/';
        
    $val preg_replace($repRegex,'$1$2'.$replacer,$val);

        
    // split the semi-serialized data into keys and values
        
    $splitRegex '/([^;}][A-Za-z0-9_]*?)\|/';
        
    $splitArray preg_split($splitRegex$val, -1,
                                    
    PREG_SPLIT_DELIM_CAPTURE);

        
    // remove offset 0 -- always empty
        
    array_shift($splitArray);

        
    // build a useful array
        
    $usefulArray = array();
        while (
    $var array_shift($splitArray)) {
            
    $val array_shift($splitArray);
            
    $val str_replace($replacer'|'$val);
            
    $usefulArray[$var] = unserialize($val);
        }
        return 
    $usefulArray;

    Basically, it turns a string of session data like:
    test|a:2:{i:0;s:5:"test1";i:1;s:5:"test2";}test2|s:3:"m|oo";

    into:

    Code:
    Array
    (
        [test] => Array
            (
                [0] => test1
                [1] => test2
            )
    
        [test2] => m|oo
    )
    I haven't completely tested this, so it may need some debugging/tweaking.
    Caveat Lector.

    S

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does unserialize() not work for this?
    Web Hound
    $x='010000010110001101101001011001000101001001100101011010010110011101101110';
    for($i=0;$i<strlen($x);$i+=8)print(chr(bindec(substr($x,$i,8))));

  3. #3
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No.
    Well, partially.

    serialized data looks like this:

    serialize("moo") -> s:3:"moo"

    the same session data looks like this:

    moo|s:3:"moo"

    Or more complex:
    test|a:2:{i:0;s:5:"test1";i:1;s:5:"test2";}test2|s:4:"m|oo";

    (an array with 2 elements, and a string).


    This function breaks the session data into separate elements (test and test2 in the above case) and then unserializes their values.

    An unserialize on straight session data returns an empty string.

    In short,
    Serialize packages one master variable (which can contain many arrays or objects, etc).
    Sessions package many masters within the same "serial string".

    S


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •