ユーザ用ツール

サイト用ツール


dokuwiki:plugin:counter

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
dokuwiki:plugin:counter [2018/07/02 06:13] matsuidokuwiki:plugin:counter [2023/05/24 03:30] (現在) matsui
行 1: 行 1:
 +====== counter ======
  
 +
 +===== アクセスカウンター =====
 +
 +今はカウンタプラグインがあって、こっちのほうが便利です。
 +[[https://www.dokuwiki.org/ja:plugin:accscounter]]
 +
 +accscounterのログディレクトリ
 +  # ll data/meta/_accscounter/
 +
 +Proxyなど使っていたりして、X-Forwad-ForからIPを取得する場合
 +  syntax/counter.php:47:            #$clientIP = clientIP(true);
 +  syntax/counter.php:48:            $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
 +
 +
 +------
 +[[http://php.s3.to/counter/|レッツPHP!]]のカウンタをDokuwikiに合わせ調節しました。
 +
 +====== lib/counter/counter.php 用意 ======
 +<code console>
 +
 +<?php
 +//ログ形式は 今日の日付け|昨日のカウント|今日のカウント|合計カウント|直前IP
 +//------------設定----------
 +//テキストカウンタなら0 画像カウンタなら1 
 +$mode = 1;
 +// 総カウント用GIF画像のディレクトリ
 +$all_path = DOKU_BASE.'lib/counter/images/';
 +// 本日カウント用GIF画像のディレクトリ
 +$day_path = DOKU_BASE.'lib/counter/images/';
 +// 昨日カウント用GIF画像のディレクトリ
 +$yes_path = DOKU_BASE.'lib/counter/images/';
 +// カウンタ記録ファイル
 +$log = dirname(__FILE__).'/all.dat';
 +// 昨日カウント数の桁数
 +$fig1 = 3;
 +// 本日カウント数の桁数
 +$fig2 = 3;
 +// 合計カウント数の桁数
 +$fig3 = 5;
 +// 連続IPはカウントしない(yes=1 no=0)
 +$ipcheck = 1;
 +//---------設定ここまで------
 +//カウント数とパスを与えて、IMGタグを返す
 +function outhtml($f_cnt, $c_path){
 +  $size = getimagesize($c_path."0.gif");  //0.gifからwidthとheight取得
 +  for ($i=0; $i<strlen($f_cnt); $i++): //桁数分だけループ
 +    $n = substr($f_cnt, $i, 1); //左から一桁ずつ取得
 +    $i_tag.="<IMG SRC=\"$c_path$n.gif\" alt=$n $size[3]>";
 +  endfor;
 +
 +  return $i_tag;
 +}
 +
 +$now_date = gmdate("Ymd", time()+9*3600); // 今日の日付
 +$yes_date = gmdate("Ymd", time()-15*3600); // 昨日の日付
 +$dat = file($log); //配列にログ読み込む
 +$ip = $_SERVER['REMOTE_ADDR'];       //IPアドレス
 +
 +//変数を展開(比較用日付、昨日、今日、総合、直前IP)
 +list($key, $yes, $tod, $all, $addr)=explode("|", $dat[0]);
 +
 +if(($ipcheck && $ip != "$addr") || $ipcheck==0){
 +  if($key == $now_date){//ログの日付が今日ならカウントアップ
 +    $tod++;
 +  }else{//日付がかわったら昨日に今日、今日に1を入れる。昨日じゃないなら昨日に0
 +    $yes = ($key == $yes_date) ? $tod : 0;
 +    $tod = 1;
 +  }
 +  $all++;//合計カウントアップ
 +  //更新
 +  $new = implode("|", array($now_date,$yes,$tod,$all,$ip));
 +  $fp = fopen($log, "w");
 +  flock($fp, LOCK_EX);
 +  fputs($fp, $new);
 +  fclose($fp);
 +}
 +//桁数整形
 +$yesterday = sprintf("%0".$fig1."d", $yes);
 +$today = sprintf("%0".$fig2."d", $tod);
 +$total = sprintf("%0".$fig3."d", $all);
 +
 +if($mode){
 +  //タグを取得(画像出力)
 +  $yesterday = outhtml($yesterday, $yes_path);
 +  $today = outhtml($today, $day_path);
 +  $total = outhtml($total, $all_path);
 +}
 +echo "Yesterday:$yesterday Today:$today Total:$total";
 +
 +  ?>
 +</code>
 +
 +
 +====== all.datを用意 ======
 +counter.phpが書き込みできるよう権限を調整
 +<code console>
 +touch lib/counter/all.dat
 +</code>
 +
 +====== 画像の用意 ======
 +フォルダ内に、0〜9.gifを設定しておく。
 +<code console>
 +mkdir lib/counter/images
 +</code>
 +
 +
 +
 +====== テンプレートに出力を記述 ======
 +
 +=== arctic テンプレート ===
 +
 +自分は、arcticのテンプレートを使用している為
 +lib/tpl/arctic/footer.html
 +
 +最下に下記を記述
 +<code>
 +<?php include(constant('DOKU_INC')."/lib/counter/counter.php");?>
 +</code>
 +
 +
 +=== dokuwiki テンプレート ===
 +
 +<code console>
 +$ diff -u lib/tpl/dokuwiki/tpl_footer.php.org  lib/tpl/dokuwiki/tpl_footer.php
 +--- lib/tpl/dokuwiki/tpl_footer.php.org 2014-12-10 09:49:21.000000000 +0900
 ++++ lib/tpl/dokuwiki/tpl_footer.php 2014-12-10 09:48:51.000000000 +0900
 +@@ -31,3 +31,4 @@
 + 
 + <?php
 + tpl_includeFile('footer.html');
 ++include(constant('DOKU_INC')."/lib/counter/counter.php");
 +</code>
 +
 +=== エラー ===
 +
 +DOKU_BASEが空なので、下記のエラーが出てしまう。
 +<code>
 +[Thu Dec 24 15:31:10 2015] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  getimagesize(/lib/counter/images/0.gif): failed to open stream: No such file or directory in /var/www/html/lib/counter/counter.php on line 26, referer: http://dokuwiki.fl8.jp/start
 +[Thu Dec 24 15:31:10 2015] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  getimagesize(/lib/counter/images/0.gif): failed to open stream: No such file or directory in /var/www/html/lib/counter/counter.php on line 26, referer: http://dokuwiki.fl8.jp/start
 +[Thu Dec 24 15:31:10 2015] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  getimagesize(/lib/counter/images/0.gif): failed to open stream: No such file or directory in /var/www/html/lib/counter/counter.php on line 26, referer: http://dokuwiki.fl8.jp/start
 +</code>
 +
 +== とりあえず下記で対応 ==
 +
 +エラー出なくするだけ
 +
 +<code>
 +# diff counter.php.org counter.php -u 
 +--- counter.php.org 2015-12-24 15:34:07.000000000 +0900
 ++++ counter.php 2015-12-24 15:32:09.000000000 +0900
 +@@ -22,7 +22,7 @@
 + //---------設定ここまで------
 + //カウント数とパスを与えて、IMGタグを返す
 + function outhtml($f_cnt, $c_path){
 +-  $size = getimagesize($c_path."0.gif");  //0.gifからwidthとheight取得
 ++  $size = @getimagesize($c_path."0.gif");  //0.gifからwidthとheight取得
 +   for ($i=0; $i<strlen($f_cnt); $i++): //桁数分だけループ
 +     $n = substr($f_cnt, $i, 1); //左から一桁ずつ取得
 +     $i_tag.="<IMG SRC=\"$c_path$n.gif\" alt=$n $size[3]>";
 +</code>
 +
 +
 +{{tag>dokuwiki}}