note 26857 added to function.session-decode
| From: | stefan at farsite dot de | Date: | Fri, 15 Nov 2002 00:21:02 +0000 |
| Subject: | note 26857 added to function.session-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39513@lists.php.net to get a copy of this message | ||
Since the functions provided by frank1970_REMOVEME_ at web dot de and mark(at)tweakt.net
weren't really working I tried the following code.
It is not nice, I know, but it does the trick. Probably some RegExp - Wiz could do it in one line.
function sess_string_to_array($sd)
{
$sess_array = Array();
//replace values within quotes since there might be ; or {} within the quotes
//these would mess up the slicing in the next step
$replaceStr = array();
$replaceStrCount = 0; //index for the saved replacement strings
$replaceParts = explode('"',$sd);
for ($i = 1; $i < count($replaceParts); $i=$i+2) {
$replaceStr[$replaceStrCount] = $replaceParts[$i];
$replaceParts[$i] = "repl_" . $replaceStrCount;
$replaceStrCount++;
}
$sd = implode('"',$replaceParts);
$vars = array();
$varCount = 0;
$flag = true;
//slice the string unsing ; and {} as separators, but keep them
while ( $flag ) {
if (strpos($sd,";") < strpos($sd,"{") || (strpos($sd,";") !==
false && strpos($sd,"{") === false )) {
$vars[$varCount] = substr($sd,0,strpos($sd,";")+1);
$sd = substr($sd,strpos($sd,";")+1,strlen($sd));
}
else if (strpos($sd,";") > strpos($sd,"{") || (strpos($sd,";")
=== false && strpos($sd,"{") !== false )) {
$vars[$varCount] = substr($sd,0,strpos($sd,"}")+1);
$sd = substr($sd,strpos($sd,"}")+1,strlen($sd));
}
if (strpos($sd,";") === false && strpos($sd,"{") === false) {
$flag = false;
}
else {
$varCount++;
}
}
//replace the quote substitutes with the real values
for ($i = 0; $i < count($vars);$i++) {
for ($k = 1; $k < count($replaceStr); $k++) {
if (strpos($vars[$i],("repl_" . $k)) !== false) {
$vars[$i] = str_replace("repl_" . $k,$replaceStr[$k],$vars[$i]) ;
}
}
}
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;
}
--
http://www.php.net/manual/en/function.session-decode.php
http://master.php.net/manage/user-notes.php?action=edit+26857
http://master.php.net/manage/user-notes.php?action=delete+26857
http://master.php.net/manage/user-notes.php?action=reject+26857