[PHP] 纯文本查看 复制代码
<?php
function check_traffic(){
//检测是否存在json文件
if(file_exists('cache/check_traffic.json')){
//检测当日json文件是否存在
if(file_exists('cache/'.date('Ymd',time()).'.json')){
//存在 读取数据并添加新的访问量
$json_data = file_get_contents('cache/'.date('Ymd',time()).'.json');
$main_json_data = file_get_contents('cache/check_traffic.json');
$arr_data = json_decode($json_data,true);
$main_arr_data = json_decode($main_json_data,true);
#当日数据
file_put_contents(
'cache/'.date('Ymd',time()).'.json',
json_encode(array('times'=>++$arr_data['times']))
);
#总数据
file_put_contents(
'cache/check_traffic.json',
json_encode(array('times'=>++$main_arr_data['times']))
);
//以数组形式返回所有访问量
$return = array($arr_data['times'],$main_arr_data['times']);
return $return;
}else{
//不存在 创建当日文件后重新调用自身
fopen('cache/'.date('Ymd',time()).'.json',"x");
file_put_contents('cache/'.date('Ymd',time()).'.json',json_encode(array('times'=>0)));
check_traffic();
}
}else{
//检测是否存在cache缓存目录
if(file_exists('cache/')){
//存在 仅创建json文件后重新调用自身
fopen('cache/check_traffic.json',"x");
file_put_contents(
'cache/check_traffic.json',
json_encode(array('times'=>0))
);
check_traffic();
}else{
//不存在 创建cache后重新调用自身
mkdir('cache');
check_traffic();
}
}
}
?>