note 76426 deleted from function.session-decode by nlopess

From: Date: Sat, 14 Jul 2007 18:42:21 +0000
Subject: note 76426 deleted from function.session-decode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-128527@lists.php.net to get a copy of this message
Note Submitter: php at mikeboers dot com ---- I needed a realiable session decoder that didnt use the session_decode function, so I also built one of my own. It has correctly parsed everything I've thrown at it that would be valid in the session ( ie. {}:;| are not allowed in the keys of the first level of the session array ) including objects and references. enjoy. <?php function session_raw_decode( $raw ) { // pull out all the strings for later preg_match_all('/s:(\d+):"(.+?)";/s', $raw, $m ) ; $strings = array() ; foreach( $m[2] as $key => $string ) { $strings[$key] = $string ; // stripSlash( $string, '"' ) ; $raw = str_replace( $m[0][$key], 's:'.$key.';', $raw ) ; } // increment all the referenced by 1 preg_match_all('/([rR]):(\d+);/', $raw, $m ) ; foreach( $m[2] as $key => $ref ) { $raw = str_replace( $m[0][$key], $m[1][$key] .':'.( $ref + 1 ) .';', $raw ) ; } // replace delimiters and format keys $split = explode('|', $raw ) ; $raw = '' ; $last = array_pop( $split ) ; foreach( $split as $key => $pair ) { if ( !preg_match('/^(.+?;)?(.+)$/', $pair, $m )) return false ; // if its the first key if( !$key ) { $m = Array('','',$pair) ; // if its an array or object } elseif ( preg_match_all( '/[aO]:\d+:/', $m[0], $a ) ) { $count = count( $a[0] ) ; preg_match( '/^((?:[^}]*?}){'.$count.'})(.+)$/', $m[0], $m ) ; } $raw .= $m[1] . ( preg_match( '/^\d+$/', $m[2] ) ? 'i:'.$m[2] : 's:'.strlen($m[2]).':"'.$m[2].'"' ) .';' ; } $raw .= $last ; // put the strings back in foreach( $strings as $key => $string ) { $raw = str_replace('s:'.$key.';', 's:'.strlen($string).':"'.$string.'";', $raw ) ; } $raw = 'a:'.(count( $split ) ).':{'.$raw.'}' ; return @unserialize( $raw ) ; } ?>

« previous php.notes (#128527) next »