A review of the session function of PHP4 (II.)

Source: Internet
Author: User
The Session_decode and Session_encode are more obscure:

BOOL Session_decode (string data);
Session_decode () Decodes the session data in data, setting variables
stored in the session.

BOOL Session_encode (void);
Session_encode () returns a string with the contents of the
Sessionencoded within.

There is no relevant example in the PHP manual. From the literal point of view decode is to put the user's string
After parsing out set to session inside, encode is the session of the data "packaging" back
to the user. It seems that encode's role may be greater, user register data to session
It should be used encode to get the data out, but the encode return things still need to be processed,
Like what:
Session_register ("Val1");
$val 1 = "ABCDE"
Session_register ("Val2");
$val 1 = 1234
Session_register ("Val3");
$val 1 = 123.45

So the thing that Session_encode returns is:

Val1:s:5: "ABCDE"; val2:i:1234;val3:d:123.45;

Obviously, ' s ' is a string, ' I ' and ' d ' is a number, ' s ' type of variable requires length.
If your session ID is aaeebbcfd4455ec2c0d5cb590f8fab74, then this string of things actually
On the existence of/tmp/aaeebbcfd4455ec2c0d5cb590f8fab74 files.
Now you need to handle this string of things yourself, to precipitate the session data you want. It's weird, php4.
Failed to provide a convenient interface to implement the resolution of the register variable ... Or I didn't find it.
Write one yourself first ...
Session_data_init take out all the "packaged" Session_data
Session_data_get get data based on variable name
Use:
$data = Session_data_init ();
$result = Session_data_get ($data, "val1");
You can take the VAL1 data out.

?
/*
* Get all date registered in the session
*/
function Session_data_init () {
$sessionData = Session_encode ();
return $sessionData;
}
?>

?
/*
* Extract one variable from enconded session data
*/
function Session_data_get ($data, $name) {
$MATCHSTR = $name. "|";
$matchStart = Strpos ($data, $MATCHSTR);
if ($matchStart = = 0) {
if (strlen ($data) < strlen ($MATCHSTR)) return "";
$TMPSTR = substr ($data, 0, strlen ($matchStr));
if (strcmp ($TMPSTR, $MATCHSTR)!= 0) return "";
}

$typeStart = $matchStart + strlen ($MATCHSTR);
$dataType = substr ($data, $typeStart, 1);

if (strcmp ($dataType, "s") = = 0) {/* String */
$lenStart = $typeStart + 2;
$lenEnd = Strpos ($data, ":", $lenStart)-1;
$lenLen = $lenEnd-$lenStart + 1;
$strLen = substr ($data, $lenStart, $lenLen);

$strStart = $lenEnd + 3;
$strResult = substr ($data, $strStart, $strLen);
return $strResult;
else if (strcmp ($dataType, "i") = = 0 | |
strcmp ($dataType, "d") = = 0) {/* number * *
$numStart = $typeStart + 2;
$numEnd = Strpos ($data, ";", $numStart)-1;
$numLen = Numend-numstart + 1;
$numResult = substr ($data, $numStart, $numLen);
return $numResult;
} else {
Return "";
}
}
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.