[2007/09] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

PHP-Text Counter

2007.09.23(Sun)

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);
}
?>

 


 

 Similar Posts:

Leave a comment

Until 24 hours, you can edit your comment from the same browser if the same PC.
But, within 24 hours, the comment will be uneditable when it has any newer comments.