|
|
 |
session_decode (PHP 4, PHP 5) session_decode -- Decodes session data from a string Descriptionbool session_decode ( string data )
session_decode() decodes the session data in
data, setting variables stored in the
session.
See also
session_encode().
User Contributed Notes
session_decode
fabrizio dot messina at gmail dot com
16-May-2005 08:40
this function _really_ split and decode session data:
function unserializesession($data) {
$vars=preg_split('/([a-zA-Z0-9]+)\|/',$data,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
for($i=0; $vars[$i]; $i++) {
$result[$vars[$i++]]=unserialize($vars[$i]);
}
return $result;
}
the difference from previously posted 'unserializesession' function is the regular expression inside function preg_split ('[a-zA-Z0-9]+' vs '[a-z,A-Z]+' )
Sasha Rudenko
16-May-2005 02:25
Here is fixed function which was described here
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;
}
I've just fix regexp, it doesn't handle names with _ (underline) sign.
But anyway, thanks for author, this function was very useful for me :)
luc at lucje dot nl
29-Sep-2004 03:39
This function decodes sessiondata:
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;
}
(there was a line left from previous code in my earlier post!)
luc at lucje dot removethisforspamplease dot nl
29-Sep-2004 02:37
I also wanted to read stores PHP sessions.
Here's my code:
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;
}
// this is just for the example
$session='/tmp/sess_'.$PHPSESSID;
// Why isn't there a sfile() function that returns a file as a string?
$h=fopen($session,'r');
$data=fread($h,filesize($h));
fclose($h);
$data=unserializesession($data);
Voila. It does the trick for me :-)
sco at postmaster dot co dot uk
04-Jun-2004 09:52
If you're trying to access your session data from outside the regular php session functions, you might want to use WDDX as your serializer, as opposed to the normal php serializer. When your data is serialized as XML, obviously it's easy to unserialize as you please.
WDDX seems to be a little slower, and the text string it creates is much bigger than that created by the normal php serializer, but it provides the functionality with minimal hassle.
Donal
forum at orthanc dot co dot nz
02-Apr-2004 02:26
Becarful using this if you are trying to switch out of an existing session rather than load one into a clean slate.
session_decode doesn't destroy the existing session data, it will over write it if there is a session variable of the same name, but if the names don't clash the existing session variables will hang around.
I have yet to find a better solution than
session_destroy()
session_start()
session_decode(....);
-----------------------------------------
To explain what I'm talking about
<?
session_start();
$a = 5;
session_register('a');
session_decode("<session that doesn't have a as a session variable>");
print (session_is_registered('a') ? $a : 'Not Registered' );
?>
The above code will print '5' as $a hasn't been destroyed or even unregistered by the session_decode
ned at wgtech dot com
02-Mar-2004 04:42
If you expected this function to return an array you are out of luck. This loads the session string into the current session. This sucks if you are doing something like managing sessions via some other function via session_set_save_handler(). I wanted to create an admin module that allowed the administration to browse the current sessions. Well, that session string is UGLY, and session_decode is utterly useless for this.
Either way here is a beasty pair of functions that returns an array that looks like $_SESSION would if you were to use session_decode. It should handle most everything, including arrays of any dimention (including arrays of arrays of arrays etc).
Call the function decodesession() and pass in the session string (the one created by session_encode). The second function, decodesessionarray(), should not be called directly. It handles decoding arrays, if any, in the string, and is recursive (it calls itself if an array stores an array).
It may not be pretty, but it works nicely.
Let me know if you use it or have any thoughts about this. It would be nice to know someone got some use out of this code!
NOTE: The code is to big to add here. To see it go to:
http://www.evilwalrus.com/viewcode.php?codeEx=553
njail
12-Dec-2003 02:28
<?PHP
$varsess = Array('SESSION');
for ($i = 0; $i < sizeof($varsess); $i++)
{
if (is_array(${"_{$varsess[$i]}"}))
{
foreach (${"_{$varsess[$i]}"} as $var=>$val)
{
$$var = $val;
}
}
unset(${"_{$varsess[$i]}"});
}
?>
petej*shaman_ca
21-Aug-2003 03:44
Seems like there was a change in the behavior of this function somewhere between 4.1.2 and 4.3.3. In 4.1.2 session_decode() didn't care whether the session was started, and would just decode the string into the _SESSION array. In my 4.3.3 install, session_decode() wouldn't work unless I explicitly started the session with session_start().
| |