customizing session handling for read write order issues

Source: Internet
Author: User
Header ("Content-type:text/html;charset=utf-8");

Define (' Session_pre ', ' xx_ ');
Define (' BR ', '
');
function Start ($savePath, $sessionName) {
Global $sessionPath;
$sessionPath = $savePath. Directory_separator;
Echo __method__. BR;
return true;
}

function Close () {
Echo __method__. BR;
return true;
}

function Read ($sessionId) {
Global $sessionPath, $sessionFile;
$sessionFile = $sessionPath. Directory_separator. Ession_pre. $sessionId;
Echo __method__. BR;
Return @file_get_contents ($sessionFile);

}

function Write ($sessionId, $data) {
Global $sessionFile;
$sessionFile = $sessionPath. Directory_separator. Ession_pre. $sessionId;
Echo __method__. BR;
Return @file_put_contents ($sessionFile, $data);
}

function Destroy ($SESSIONID) {
Global $sessionFile;
@unlink ($sessionFile);
Echo __method__. BR;
return true;
}

Function GC ($maxLifeTime) {
Echo __method__. BR;
Global $sessionPath;
foreach (Glob ($sessionPath. " /* ") as $file) {
if (Filemtime ($file) + $maxLifeTime
@unlink ($file);
}
}
return true;
}
Session_set_save_handler ("Start", "Close", "read", "write", "Destroy", "GC");

Session_Start (); 101 Rows
$_session[' name '] = ' lielei '; 102 rows
Print_r ($_session); 103 rows
?>

Problem Description:
As I understand Session_Start () is turned on, the function that runs in sequence is start, read, write, close.
Official information: PHP calls the Write function after the script has finished executing or after calling the Session_write_close () function.

So why would you output lilei the first time you run this script to 103 rows, when the data in $_session is
Read from the session file? If so, is the script not over yet, and how does it execute write? If not,
Is the $_session just equivalent to an ordinary array at this point? 103 rows is just a normal array of output?


Reply to discussion (solution)

Ask for explanations

Output in sequence
Start
Read
Array ([name] = = Lielei)
Write
Close
What's wrong with it?

Print_r ($_session); After that,?>.
Does that mean the program is over?

$_session is a hyper-global array that can appear anywhere in the program without declaration

Output in sequence
Start
Read
Array ([name] = = Lielei)
Write
Close
What's wrong with it?

Print_r ($_session); After that,?>.
Does that mean the program is over?

$_session is a hyper-global array that can appear anywhere in the program without declaration


If Print_r ($_sesson), add a few more statements later.
I mean the first time to run the program to run to 103 lines, add the following there is code, how to $_session the surface has value?

1, $_sesson in Session_Start (); Generated at execution time
2, you are in Print_r ($_session); Add several echo ' xxxx ' in the back; No, you see that?
3, you are $_session[' name '] = ' lielei '; , how could $_session have no value?

1, $_sesson in Session_Start (); Generated at execution time
2, you are in Print_r ($_session); Add several echo ' xxxx ' in the back; No, you see that?
3, you are $_session[' name '] = ' lielei '; , how could $_session have no value?


I just want to figure out its internal execution principle, session_start () after executing a number of functions, I think the official is: PHP will call the Write function after the script finishes or calls the Session_write_close () function.

So the first print, then the end of the program, I'm talking about this execution process, since the first execution of print, then did not execute write, how to output it
I don't understand.


1, $_sesson in Session_Start (); Generated at execution time
2, you are in Print_r ($_session); Add several echo ' xxxx ' in the back; No, you see that?
3, you are $_session[' name '] = ' lielei '; , how could $_session have no value?


I just want to figure out its internal execution principle, session_start () after executing a number of functions, I think the official is: PHP will call the Write function after the script finishes or calls the Session_write_close () function.

So the first print, then the end of the program, I'm talking about this execution process, since the first execution of print, then did not execute write, how to output it
I don't understand.


$_session is in memory and Session_write_close () writes session data to the session file on the server side. So before you write to the file, the values and settings for the session are for the $_session variable.

Not a bunch of faces, no? Line Session_write_close (); So, no? Update to session file, what do you take? The value of the session that is stored.

Well, I guess so, but just guessing, so come and ask, thanks for the answer.

  • Related Article

    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.