一个防刷新的文本计数器- -
这个不错吧!
找了很久的,大家用!
function count_txt($id) {
$ip = getenv("REMOTE_ADDR");
$currenttime = time();
$file_count = "count.txt";
$file_ipandtime = "ipandtime.txt";
checkExists($file_count,$file_ipandtime);
$date = file($file_ipandtime);
$countdate = count($date);
for($i = 0; $i<$countdate; $i++){
$info=explode("|",$date[$i]);
//$info[0]:ip, $info[1]:id, $info[2]:etime
if($info[0]==$ip&&$info[1]==$id){
$timestamp = $info[2];
if($currenttime-$timestamp > 3600){//change the time-slot as you like
$fp = fopen($file_ipandtime,"r+");
$filesize = filesize($file_ipandtime);
$content = fread($fp,$filesize+1);
$content = str_replace("$ip|$id|$timestamp","$ip|$id|$currenttime",$content);
rewind($fp);
fwrite($fp,$content);
del($timestamp);
fclose($fp);
$update = TRUE;
$count = Updatecount($id, $update);
return ($count);
}
del($timestamp);
$update = FALSE;
$count = Updatecount($id, $update);
return($count);
exit();
}
}
del($timestamp);
$fp = fopen($file_ipandtime,"a");
$add_db = "$ip|$id|$currenttime| ";
fwrite($fp,$add_db);
fclose($fp);
$update = TRUE;
$count = Updatecount($id, $update);
return ($count);
}
//------------------------------------------
function checkExists($file_count,$file_ipandtime){
if(!file_exists($file_count)){
$fp = fopen($file_count, "w+");
//add some item more here as this mode
$str1 = "1|0| "."1|1| "."1|2| ";
fputs($fp,$str1);
fclose($fp);
}
if(!file_exists($file_ipandtime)){
$fp = fopen($file_ipandtime, "w+");
fclose($fp);
}
}
//------------------------------------------
function Updatecount($id, $update){
$file_count = "count.txt";
$fp = fopen($file_count,"r+");
$date = file($file_count);
$countdate = count($date);
if($update==TRUE){
for($i = 0; $i < $countdate; $i++){
$info=explode("|",$date[$i]);
//$info[0]:count,$info[1]:id
if($info[1]==$id){
$oldcount = $info[0];
$currentcount = ++$info[0];echo "$count";
$filesize = filesize($file_count);
$content = fread($fp,$filesize+1);
$content = str_replace("$oldcount|$id","$currentcount|$id",$content);
rewind($fp);
fwrite($fp,$content);
}
}
}
for($i = 0; $i < $countdate; $i++){
$info = explode("|",$date[$i]);
//$info[0]:count,$info[1]:id
if($info[1]==$id){
$count = $info[0];
return $count;
}
}
}
//----------------------------------------
function del($timestamp) {
$file_ipandtime = "ipandtime.txt";
$currenttime = time();
$date = file($file_ipandtime);
$countdate = count($date);
for($i = 0; $i < $countdate; $i++){
if($currenttime-$timestamp > 3600){//change the time-slot as you like
$info = explode("|", $date[$i]);
$ip = $info[0];
$id = $info[1];
$timestamp = $info[2];
$fp = fopen($file_ipandtime,"r");
$filesize = filesize($file_ipandtime);
$content = fread($fp,$filesize+1);
$content = str_replace("$ip|$id|$timestamp| ","",$content);
$content = trim("$content");
fclose($fp);
$fp = fopen($file_ipandtime,"w+");
fwrite($fp,$content);
fclose($fp);
}
}
}
/*==========AUTHOR=============
ID:antPlus QQ:38188141
============================*/
?>