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

session_start();能导致flock失败

WBOY
WBOYOriginal
2016-06-13 13:13:161198browse

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); 


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

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

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

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

Statement:
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