PHP session read/write lock

Source: Internet
Author: User

PHP session read/write lock

First look at the example, the function:

1. Click on a Button,ajax in the page to run the php,php with the session record to run to which step.


2. Using AJAX polling There is also a PHP that gets the data in session, and the output runs to which step.


session.html call PHP to run, and output run to the first few steps

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
handle.php Run and record the run to the first few steps

<?phpsession_start (); $_session[' step '] = "; $n = 1;while ($n <=10) {    $_session[' step '] = $n;    Sleep (1);    $n + +;} $_session[' Step ' = ' complete ';?

>


getstep.php get run to the first few steps

<?phpsession_start (); Echo isset ($_session[' step ')? $_session[' Step ']: ';?

>


the runtime finds that not every step is returned, but instead waits 10 seconds to return to complete immediately.


When you run session_start () , the session is locked. Until the page finishes running.


So the page runs in the meantime. Writing to the sesssion is only stored in memory. does not write to the session file.

While reading the session, you need to wait. Until the session lock unlocks the ability to read.


We can use session_write_close () to write data to the session file and end the session process. This will not have to wait for the page to finish running, but also to get to where to run.

But there's a problem with that. Is after the Sesssion_write_close () is finished running. No matter what the write operation of the session does not work. Because the session process has ended.

Therefore, the session needs to be written again. Add Session_Start () in front


session_start -start New or resume existing session

Session_write_close -write session data and end session


handle.php The following changes, you can get to which step to run

<?phpsession_start (); $_session[' step '] = "; $n = 1;while ($n <=10) {    $_session[' step '] = $n;    Session_write_close (); Writes the data to the session file and ends the session process    session_start ();       Create session process again    sleep (1);    $n + +;} $_session[' Step ' = ' complete ';? >


PHP session read/write lock

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.