note 44837 deleted from function.unserialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 01:18:31 +0000 |
| Subject: | note 44837 deleted from function.unserialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191689@lists.php.net to get a copy of this message | ||
Note Submitter: eoin at joy dot ucc dot ie
----
I recently found myself in need of unserializing PHP session data (stored in a database) that had
expired but was not yet deleted i.e. I needed to get at the contents of the session but was unable
to use the usual PHP interface. The following function takes a string of serialized session data and
returns an array of PHP values. The structure of the returned array is the same as the $_SESSION
array if you were using the normal interface. I haven't tested this extensively but it did the
job for me.
<?php
function unserialize_session_data( $serialized_string ) {
$variables = array( );
$a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE );
for( $i = 0; $i < count( $a ); $i = $i+2 ) {
$variables[$a[$i]] = unserialize( $a[$i+1] );
}
return( $variables );
}
?>