Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
会话开始与php中的usleep冲突_Php_Ajax_Session_Usleep - Fatal编程技术网

会话开始与php中的usleep冲突

会话开始与php中的usleep冲突,php,ajax,session,usleep,Php,Ajax,Session,Usleep,我想写一个聊天系统。我使用过ajax、php和commet porotocol 一切正常,但会话有一个问题:当会话在脚本顶部启动时,一切都出错(我的脚本无法启动,并且有一条新消息,所以我必须等待睡眠时间结束) 这是我的php文件的简单版本: $filename = dirname(__FILE__).'/data.txt'; // store new message in the file $msg = isset($_GET['msg']) ? $_GET['msg'] : '';

我想写一个聊天系统。我使用过ajax、php和commet porotocol

一切正常,但会话有一个问题:当会话在脚本顶部启动时,一切都出错(我的脚本无法启动,并且有一条新消息,所以我必须等待睡眠时间结束)

这是我的php文件的简单版本:

   $filename  = dirname(__FILE__).'/data.txt';

// store new message in the file
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
  file_put_contents($filename,$msg);
  die();
}
// infinite loop until the data file is not modified
$lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
  usleep(10000); // sleep 10ms to unload the CPU
  clearstatcache();
  $currentmodif = filemtime($filename);
}

// return a json array
$response = array();
$response['msg']       = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
$filename=dirname(_文件)'/data.txt';
//在文件中存储新消息
$msg=isset($\u GET['msg'])$_获取['msg']:'';
如果($msg!='')
{
文件内容($filename,$msg);
模具();
}
//无限循环,直到数据文件未修改
$lastmodif=isset($\u GET['timestamp'])$_获取['timestamp']:0;
$currentmodif=filemtime($filename);

虽然($currentmodif这是因为会话处于锁定状态,但避免这种情况的唯一方法是在usleep命令之前调用
session\u write\u close()

基本上,如果一个脚本使用会话,则在第一个脚本完成或调用
session\u write\u close()之前,不能同时为使用相同web浏览器的同一客户端运行其他脚本
。这是因为PHP使用锁定,并且看到会话文件被锁定,并在运行脚本之前等待它再次可用