Categories: php script

PHP-Text Counter

i made a PHP-Text Counter for i-mode. when we access the net by mobile phones, the host name/ip address is frequently changed. so i use ‘USER_AGENT’ and ‘TIME’ elements for preventing double-count. like this.

 

 

<?php
 $filename = “./counter.dat”; // datのパス

 $UA = htmlspecialchars($_SERVER[‘HTTP_USER_AGENT’]);//ユーザーエージェントの取得
 if (strncmp($UA, “Mozil”, 5) <>0 && strncmp($UA, “Opera”, 5) <>0 && strncmp($UA, “Lynx/”, 5) <>0 && strncmp($UA, “HotJa”, 5) <>0) {
 $HIJI = date(“U”);//アクセス年月日時刻の取得
 $file = fopen($filename,”r+”);
 flock($file, 2);
 $count = fgets($file,256);//直前のカウント数の取得
 $lastUA = fgets($file,256);//直前のユーザーエージェントの取得
 $lastHIJI = fgets($file,256);//直前のアクセス年月日時刻の取得
 $count = chop($count);
 $lastUA = chop($lastUA);
 $lastHIJI = chop($lastHIJI);
 $dffrHiJI = $HIJI – $lastHIJI;
 if($lastUA != $UA)                        //ユーザーエージェントが直前のものと異なる場合カウントアップ
 {$count = $count + 1;
 $lastUA = $UA;
 $lastHIJI = $HIJI;
 } elseif($dffrHiJI > 1200)               //ユーザーエージェントが直前のものと同じでも20分以上経っている場合はカウントアップ
 {$count = $count + 1;
 $lastHIJI = $HIJI;}
 ftruncate($file,0);
 rewind($file);
 fwrite($file, $count);
 fwrite($file, “\n”);
 fwrite($file, $lastUA);
 fwrite($file, “\n”);
 fwrite($file, $lastHIJI);
 flock($file, 3);
 fclose($file);
}
?>

o6asan

Share
Published by
o6asan

Recent Posts

Very easy, sweet potato yōkan recipe.

The recipe for sweet potato yōkan that I often made this fall. Ingredients Sweet potato…

1 year ago

On my home network, I’ve built an FTP server that I left for long time.

After a long time, when I checked broken links and fixed them, I got an…

3 years ago

Moving “o6asan’s BBS”.-#2

I made a box, so I prepare the contents. Theme and Plugins. The theme is…

3 years ago

Moving “o6asan’s BBS”.

Hehe, it's been almost a year since my last post. I received a notification email…

3 years ago

I completed my WordPress to Sub-domain.

About a week ago, I finally started to renew my sites, which I had been…

4 years ago

Happy New Year!

   Happy New Year! It is the beginning of a new year.    This is a year…

6 years ago

This website uses cookies.