Home > 備忘録 > 言語関連 > php に関すること > アクセスカウンター( 19 )
はじめて、classを利用してアクセスカウンターを作ってみました。
class AccessCount{
const COUNT_DIR = "./countDB/"; //データの保存ディレクトリ
const COUNT_FILE = "count.dat"; //ファイル名
private $start_date;
private $today_date;
private $today;
private $yesterday;
private $total;
private $ip;
private $falg;
function __construct() {
if(!opendir(self::COUNT_DIR)){ //なければ作成
mkdir (self::COUNT_DIR, 0777);
}
$this->start_date=mktime(0,0,0,5,10,2012);//2012年5月10日(カウンタ設置日付)
$this->falg=true; //同じIPアドレスからの連続アップ禁止フラッグ
$fname=self::COUNT_DIR.self::COUNT_FILE;//アクセスカウンターのファイル名
if(!file_exists($fname)){//存在していなければ初期化
$this->today_date = date("Ymd",time()-24*3600);
$this->today=0;
$this->yesterday=0;
$this->total=0;
$this->ip = 0;
}else{
$fp = fopen($fname,"r");//カウントデータファイルを開く
flock($fp,LOCK_SH);
$temp = fgets($fp);//データを読み込む
fclose($fp);
list($this->today_date,$this->today,$this->yesterday,$this->total,$this->ip)=explode(",",$temp);
}
}
function __destruct() {
}
private function _increment(){
if(date("Ymd")!=$this->today_date){ //日付が変わった時
$this->today_date = date("Ymd");
$this->yesterday = $this->today;
$this->today = 1;
}else{
$this->today++;
}
$this->ip = $_SERVER['REMOTE_ADDR'];
$this->total++;
$fname=self::COUNT_DIR.self::COUNT_FILE; //アクセスカウンターのファイル名
if(!file_exists($fname)){ //存在していなければ作成する
$fp = fopen($fname,"w");
$temp = implode(",",array($this->today_date,$this->today,$this->yesterday,$this->total,$this->ip));
fputs($fp,$temp); //値を書き込む
fclose($fp); //カウントファイルを閉じる
chmod ($fname, 0777); //データベースの属性を変更
chmod (self::COUNT_DIR, 0777); //保存ディレクトリの属性を変更
}else{
$temp = implode(",",array($this->today_date,$this->today,$this->yesterday,$this->total,$this->ip));
$fp = fopen($fname,"w");
flock($fp,LOCK_EX);
fputs($fp,$temp);//値を書き込む
fclose($fp);//ファイルを閉じる
}
}
function increment(){
if($this->falg){
if($this->ip != $_SERVER['REMOTE_ADDR']){ //前回のIPと異なる場合にカウントアップ
$this->_increment();
}
}else{
$this->_increment();
}
}
function get_list(){
return array($this->today_date,$this->today,$this->yesterday,$this->total,$this->ip);
}
function get_count(){
return array($this->today,$this->yesterday,$this->total);
}
function get_today(){
return $this->today;
}
function get_yesterday(){
return $this->yesterday;
}
function get_total(){
return $this->total;
}
function get_start_date(){
return $this->start_date;
}
function get_ip(){
return $this->ip;
}
function get_average(){ //前日までの平均
$date=mktime(0,0,0,date("n"),date("d"),date('Y'));
$days=($date-$this->start_date)/86400;
if($days==0) return 0;
return round(($this->total-$this->today)/$days,1);
}
}次のいづれかの方法で、表示させる。
$obj=new AccessCount();
$obj->increment();
$ret="今日:".number_format($obj->get_today())." 昨日:".number_format($obj->get_yesterday())." 累計:".number_format($obj->get_total())." since ".date("Y/n/d",$obj->get_start_date());
echo $ret;$obj=new AccessCount();
$obj->increment();
list($today_date,$today,$yesterday,$total,$ip)=$obj->get_list();
$ret="今日:".number_format($today)." 昨日:".number_format($yesterday)." 累計:".number_format($total)." since ".date("Y/n/d",$obj->get_start_date());
echo $ret;$obj=new AccessCount();
$obj->increment();
list($today,$yesterday,$total)=$obj->get_count();
$ret="今日:".number_format($today)." 昨日:".number_format($yesterday)." 累計:".number_format($total)." since ".date("Y/n/d",$obj->get_start_date());
echo $ret;