PHP session Read and Write lock example

Source: Internet
Author: User
Tags php session sessions sleep

First look at an example, function:

1. Click on a button in the page, Ajax execution php,php with the session record to which step.

2. Use Ajax to poll another PHP, get the data in session, and where to go.

Session.html invokes PHP execution and outputs the execution 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 execute and record the execution to the first few steps

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

getstep.php get execution to the first few steps

<?php  
session_start ();  
echo isset ($_session[' step ')? $_session[' Step ']: ';  
? >

When executed, not every step is returned, but wait 10 seconds to return directly to complete.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

When the session_start () is executed, the session is locked. Until the page execution completes.

So in the course of the page execution, writes to the sesssion are saved only in memory and are not written to the session file.

When you read the session, you need to wait until the session lock is unlocked before you can read it.

We can use Session_write_close () to write data to the session file and end the sessions process. This does not require you to wait for the page to complete, or to get to the next step of execution.

The problem, however, is that any write to the session does not work after Sesssion_write_close () has been executed. Because the session process has ended.

So when you need to write the session again, precede the session_start ()

Session_start-start New or resume existing session

Session_write_close-write session data and end session

Handle.php The following modifications, you can get to the step of execution

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

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.