Home > Backend Development > PHP Tutorial > session_start();能导致flock失败

session_start();能导致flock失败

WBOY
Release: 2016-06-13 13:13:16
Original
1232 people have browsed it

session_start();会导致flock失败?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php session_start();
 
$file = "temp.txt";   
$fp = fopen($file , 'w');   
if(flock($fp , LOCK_EX | LOCK_NB)){   
    sleep(10); 
    echo 'a';
    flock($fp , LOCK_UN);   
} else{   
    echo "Lock file failed...\n";   
}   
fclose($fp); 
Copy after login


以上代码很简单了。保存为a.php 打开两个窗口。都运行a.php第一个会等10秒。第二个会输出lock file failed 但加了session_start后。都会卡住。而不是第二个很快输出lock file failed。请问是何原理?
目前必须要用到session_start也必须要用到flock。

------解决方案--------------------
你在 session_start(); 之后加入
echo session_id();

出现卡死现象时,一定是输出相同的 sessionid 的
session 临时文件是需要加锁的,这是保证唯一性的需要

因此,不要怀疑php开发团队的智商

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template