📄 08c09-3.php
字号:
<?php// A function to creating a lock directory for us, getting around// the need to use flock()function lock($filename) { // Remove the ability for user aborts until we are finished ignore_user_abort(true); // Start a counter, we only want to try to obtain a lock // a certain number of times: $counter = 0; // Until we get a lock, or die trying, go for it: do { // Sleep for counter-squared seconds, longer each time: sleep($counter * $counter); // Make the directory: $success = @mkdir("{$filename}.dirlock"); } while ( !($success) && ($counter++ < 10) ); // If counter is 11, then we never got the lock: if ($counter == 11) { die('ERROR: Could not get exclusive lock!'); } // Otherwise we now have the lock and are done!}// Now create a function that will handle unlocking for us:function unlock($filename) { // Remove the directory: if (!(rmdir("{$filename}.dirlock"))) { die('ERROR: Could undo exclusive lock!'); } // Remove the restriction on user aborts: ignore_user_abort(false);}// Now use these functions to do a full read/write loop on a file// incrementing a number for each time the file is written:// Obtain the lock:$lock = lock('data.counter');// Read the file, determine our count, and write the data:$filedata = @file('data.counter');$count = isset($filedata[0]) ? $filedata[0] + 1 : 1;file_put_contents('data.counter', $count);// Release the exclusive lockunlock('data.counter');// Now echo out our file contents so that we can see what happened:echo '<pre>';readfile('data.counter');echo '</pre>';?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -