← 記事に戻る
# アクセスカウンターはSQLiteとPHP言語で出来ています.

おはようございます、このサイトのアクセスカウンターはSQLiteとPHP言語で出来ています.ボット訪問者はカウントしないなどは別のプログラムで制御しています.そういう訳もあって結構シンプルなコードで出来上がっていると思います.この頃は海外からの訪問者も増えてきています.とくに中国とシンガポール経由の訪問者です.恐らくボットだという事は分かっているのですが、ボットと判定しづらい方法でアクセスしているので、アナリティクス(アクセス解析)にもカウントされます.

それが少々困っています.毎分でアクセスしてくるので非常にサーバーに負荷がかかっているのは分かっているのですが、どうしようもない.国ごとアクセスを遮断するプログラムをそのうち入れようと思っています.

一番簡単な遮断方法は[MaxMind](https://github.com/maxmind)のPHPライブラリを使えば良いらしい.これを使用するとIPから何処の国からアクセスしてきたか判別可能になります.だけど使用するにはライブラリの他にデータベース(IP)をダウンロード必要があり、ダウンロードするにはMaxMindでアカウントを登録しないデータベースがダウンロード出来ないようになっているので自分はそこで止まっています.規約がなぁ….。

```
<?php

require 'vendor/autoload.php';//絶対パスが宜しいかと

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;

class counter
{
    public $targetId = 1;
    public $cnt = 0;
    public $dbPath = 'counter/cnt.db';//絶対パスが宜しいかと

    public function __construct($dbPath = null,$targetId=null)
    {
        $dbPath = $dbPath ?? $this->dbPath;
        $this->targetId = $targetId??$this->targetId;

        // DBファイルが無ければ作成(空ファイル)
        if (!file_exists($dbPath)) {
            touch($dbPath);
        }

        // Capsuleインスタンス
        $capsule = new Capsule();

        // SQLite接続
        $capsule->addConnection([
            'driver'   => 'sqlite',
            'database' => $dbPath,
            'prefix'   => '',
        ]);

        $capsule->setAsGlobal();
        $capsule->bootEloquent();

        /**
         * zcnt テーブルが無ければ作成
         */
        if (!DB::schema()->hasTable('cnt_tbl')) {
            DB::schema()->create('cnt_tbl', function (Blueprint $table) {
                $table->integer('id')->primary();
                $table->integer('cnt');
            });

            // 初期データ投入
            DB::table('cnt_tbl')->insert([
                'id'  => $this->targetId,
                'cnt' => 1,
            ]);
        }

        return $this;
    }

    public function getCounter()
    {
        $zcnt = DB::table('cnt_tbl')->where('id', $this->targetId)->first();
        $this->cnt = $zcnt?->cnt;
        return $this;
    }

    public function setCounter()
    {
        if (!is_null($this->cnt)) {
            $this->cnt++;
            DB::table('cnt_tbl')->where('id', $this->targetId)->update([
                'cnt' => $this->cnt
            ]);
        }
    }

    public function output()
    {
        print json_encode([
            'cnt' => $this->cnt
        ]);
    }
}

```



明日へ続く

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-32.png) 成功体験と成功の模倣.

 ](https://zip358.com/2026/02/05/%e6%88%90%e5%8a%9f%e4%bd%93%e9%a8%93%e3%81%a8%e6%88%90%e5%8a%9f%e3%81%ae%e6%a8%a1%e5%80%a3.html) 

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-31.png) サイトを分離した話を黙々と書いていきます.

 ](https://zip358.com/2026/02/04/%e3%82%b5%e3%82%a4%e3%83%88%e3%82%92%e5%88%86%e9%9b%a2%e3%81%97%e3%81%9f%e8%a9%b1%e3%82%92%e9%bb%99%e3%80%85%e3%81%a8%e6%9b%b8%e3%81%84%e3%81%a6%e3%81%84%e3%81%8d%e3%81%be%e3%81%99.html) 

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-30-300x300.png) 半額セールでアドビ様を延長.

 ](https://zip358.com/2026/02/03/%e5%8d%8a%e9%a1%8d%e3%82%bb%e3%83%bc%e3%83%ab%e3%81%a7%e3%82%a2%e3%83%89%e3%83%93%e6%a7%98%e3%82%92%e5%bb%b6%e9%95%b7.html) 

 [ ![](https://zip358.com/wp-content/uploads/2026/01/image-29.png) 何の前触れもなく変えたのは初めてかも.

 ](https://zip358.com/2026/02/02/%e4%bd%95%e3%81%ae%e5%89%8d%e8%a7%a6%e3%82%8c%e3%82%82%e3%81%aa%e3%81%8f%e5%a4%89%e3%81%88%e3%81%9f%e3%81%ae%e3%81%af%e5%88%9d%e3%82%81%e3%81%a6%e3%81%8b%e3%82%82.html) 

 [ ![](https://zip358.com/wp-content/uploads/2023/01/you-300x300.png) 跳ねたいサイトで跳ねたい

 ](https://zip358.com/2026/02/01/%e8%b7%b3%e3%81%ad%e3%81%9f%e3%81%84%e3%82%b5%e3%82%a4%e3%83%88%e3%81%a7%e8%b7%b3%e3%81%ad%e3%81%9f%e3%81%84.html)