Session function of php4 (2)

Source: Internet
Author: User

Session_decode and session_encode are vague:

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 current
Sessionencoded.

There seems to be no relevant example in php manual. In the literal sense, decode refers to the user's string
After the data is parsed, set it to the session. The encode is to "package" the session data and return it.
To the user. In this case, the encode may play a greater role, and the user registers data to the session.
In the future, we should use encode to extract data, but the content returned by encode still needs to be processed,
For example:
Session_register ("val1 ");
$ Val1 = "abcde"
Session_register ("val2 ");
$ Val1 = 1234
Session_register ("val3 ");
$ Val1 = 123.45

The returned result of session_encode is:

Val1: s: 5: "ABC"; val2: I: 1234; val3: d: 123.45;

Obviously, 's' is string, 'I' and 'D' are numbers, and the length of the 'S' type variable needs to be long.
If your session id is aaeebbcfd4455ec2c0d5cb590f8fab74, then this string is actually
The file/tmp/aaeebbcfd4455ec2c0d5cb590f8fab74 exists.
Now you need to process this string and analyze the session data you want. It's strange that php4
I didn't provide a convenient interface for parsing the register variable... or I didn't find it.
Write one by yourself...
Session_data_init: extracts all "packaged" session_data
Session_data_get get data based on the variable name
Usage:
$ Data = session_data_init ();
$ Result = session_data_get ($ data, "val1 ");
The val1 data can be retrieved.

<?
/*
* 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.