一个防刷新的文本计数器 - 我爱贝贝 - 金东浩官方网站 - welcome to old.bbmy.net

一个防刷新的文本计数器 [金东浩官方网站版权所有]

2006年12月31日 | Category: » | by: 贝爸 | 本文包含金东浩一个防刷新的文本计数器照片

估计会有BUG,仅供参考


<?
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

       }
   }
}
?>

本文来自 我爱贝贝[www.bbmy.net]-金东浩官方网站,如若转载注明出处。


金东浩博客随机日志
金东浩博客之一首很不错的小文
一首很不错的小文
金东浩博客之小贝有了贝雷帽
小贝有了贝雷帽
金东浩博客之下了几个多图边框
下了几个多图边框
金东浩博客之我们家二月一就龙抬头
我们家二月一就龙
金东浩博客之剃头的第二天,二月二
剃头的第二天,二
金东浩博客之小贝春天的壁纸出炉了
小贝春天的壁纸出
金东浩博客之春天来了,小贝包河游
春天来了,小贝包
金东浩博客之奶瓶篇——包河游系列之一
奶瓶篇——包河游
金东浩博客之风车篇——包河游系列之一
风车篇——包河游
金东浩博客之踏春篇——包河游系列之一
踏春篇——包河游
金东浩博客之学步篇——包河游系列之一
学步篇——包河游
金东浩博客之棉花糖篇——包河游系列之一
棉花糖篇——包河
金东浩博客之打预防针的小贝
打预防针的小贝
金东浩博客之关于哄睡觉
关于哄睡觉
金东浩博客之胜利广场之蒙牛城市之间
胜利广场之蒙牛城
金东浩博客之贝贝与蒙牛多多
贝贝与蒙牛多多
金东浩博客之贝贝快要自已走路了
贝贝快要自已走路
金东浩博客之小贝发现的城堡
小贝发现的城堡

金东浩日记Tags:
金东浩日记评论: 0 | 金东浩日记引用: 0 | 金东浩日记阅读: 2756
发表评论
昵称 [注册]        网址
密码 游客无需密码     电邮
打开HTML 打开UBB 打开表情 隐藏 记住我