ajax1.html三个ajax方法同时访问ajax1.php,ajax1.php中有进行文件data.php读写,由于三个ajax方法访问频率特别高,就产生了并发访问,导致读写出错,使用了flock()还是会出错,请高手们指导一下怎么解决呢?
ajax1.html代码:
var a = 1;
var b = 1;
var c = 1;
function ajax1(){
$.get('ajax1.php?from=a&value='+a, function(res){
$('#ajax1').text(a);
a++;
if(res == 1){
ajax1();
}
});
}
function ajax2(){
$.get('ajax1.php?from=b&value='+b, function(res){
$('#ajax2').text(b);
b++;
if(res == 1){
ajax2();
}
});
}
function ajax3(){
$.get('ajax1.php?from=c&value='+c, function(res){
$('#ajax3').text(c);
c++;
if(res == 1){
ajax3();
}
});
}
function beginAjax(){
ajax1();
ajax2();
ajax3();
}
ajax1.php代码:
$from = $_GET['from'];
$value = $_GET['value'];
$data = is_array(include 'data.php')? include 'data.php': array();
$data[] = $from .'-'. $value;
$file = fopen('data.php', 'w');
$lock = flock($file, LOCK_EX);
if($lock){
fwrite($file, '<?php');
fwrite($file, PHP_EOL);
fwrite($file, 'return ');
fwrite($file, var_export($data, true));
fwrite($file, ';');
flock($file, LOCK_UN);
}
fclose($file);
exit('1');
data.php代码(以下数据是出错了的数据):
return array (
0 => 'b-3',
);1 => 'a-1',
2 => 'c-1',
3 => 'b-2',
4 => 'a-2',
5 => 'c-2',
);
- 4 回答
- 0 关注
- 387 浏览
添加回答
举报
0/150
提交
取消