Custom session processing readwrite sequence

Source: Internet
Author: User
Custom session processing readwrite sequence 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); // row 103
?>

Problem description:
According to my understanding, after session_start () is enabled, start, read, write, and close functions are run in sequence.
Official materials: PHP will call the write function after the script is executed or session_write_close () function is called.

So why does Lilei output when this script is run to 103 rows for the first time? the data in $ _ SESSION is
Read from the session file? If yes, isn't the script over yet? how can I execute write? If not,
Is $ _ SESSION equivalent to a normal array? Row 3 outputs a normal array?


Reply to discussion (solution)

Explanations

Output in sequence
Start
Read
Array ([name] => Lielei)
Write
Close
Why not?

Print_r ($ _ SESSION); then?>
Does this mean that the program is over?

$ _ SESSION is a super Global Array, which can appear anywhere in the program without declaration.

Output in sequence
Start
Read
Array ([name] => Lielei)
Write
Close
Why not?

Print_r ($ _ SESSION); then?>
Does this mean that the program is over?

$ _ SESSION is a super Global Array, which can appear anywhere in the program without declaration.


If print_r ($ _ SESSON); then add a few more statements.
I mean the first time I run the program to 103 rows, I add the following code. why is there a value for $ _ session?

1. $ _ SESSON is generated during session_start (); execution
2. you have added several echo 'xXx' To print_r ($ _ SESSION); and you will not see it.
3. you all $ _ SESSION ['name'] = 'lielei '; now, why does $ _ SESSION have no value?

1. $ _ SESSON is generated during session_start (); execution
2. you have added several echo 'xXx' To print_r ($ _ SESSION); and you will not see it.
3. you all $ _ SESSION ['name'] = 'lielei '; now, why does $ _ SESSION have no value?


I just want to figure out its internal execution principle. after session_start (), multiple functions are executed. I think the official website is: PHP will finish executing the script or call session_write_close () call the write function.

Then print is executed first, and then the program is finished. I am talking about this execution process. Since print is executed first, no write has been executed. how can this problem be output?
I don't understand.


1. $ _ SESSON is generated during session_start (); execution
2. you have added several echo 'xXx' To print_r ($ _ SESSION); and you will not see it.
3. you all $ _ SESSION ['name'] = 'lielei '; now, why does $ _ SESSION have no value?


I just want to figure out its internal execution principle. after session_start (), multiple functions are executed. I think the official website is: PHP will finish executing the script or call session_write_close () call the write function.

Then print is executed first, and then the program is finished. I am talking about this execution process. Since print is executed first, no write has been executed. how can this problem be output?
I don't understand.


$ _ SESSION exists in the memory, and session_write_close () writes session data to the session file on the server. Therefore, before writing a file, your values and settings for the session are for the $ _ SESSION variable.

? Face? Bundle, no ?? Row session_write_close (); so no? Update to session file, you? Only? The value of the stored session.

Well, I guess so too, but I only guess. so let's ask. thank you for your answer.

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.