mmdm / sim-hit-counter
一个简单而不错的点击计数器库
Requires
- ext-json: *
- ext-pdo: *
- jenssegers/agent: ^2.6
This package is auto-updated.
Last update: 2024-09-29 05:56:02 UTC
README
一个用于统计用户点击的简单库。
注意
请注意,这个库仅适用于小型网站,所以...请不要用于高流量网站,并使用更专业的工具来处理大量数据库和服务器负载。
安装
composer
composer require mmdm/sim-hit-counter
或者您可以直接从github下载zip文件,解压后将其放置到您的项目库中,然后像使用其他库一样使用它。
只需添加以下行到autoload文件
require_once 'path_to_library/autoloader.php';
然后您就可以使用了。
架构
此库使用数据库以获得最佳性能
排序规则
应该是 utf8mb4_unicode_ci,因为它是一个非常不错的排序规则。有关 utf8 和 utf8mb4 在 general 和 unicode 之间的差异的更多信息,请参阅 此链接 来自 stackoverflow
表
-
hits
此表包含所有点击。
此表的最低列数应该是
-
id (INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT)
-
url (TEXT NOT NULL)
-
type (VARCHAR(20) NOT NULL)
-
view_count (BIGINT(20) UNSIGNED NOT NULL DEFAULT 0)
-
unique_view_count (BIGINT(20) UNSIGNED NOT NULL DEFAULT 0)
-
from_time (INT(11) UNSIGNED NOT NULL)
-
to_time (INT(11) UNSIGNED NOT NULL)
-
如何使用
首先,您需要一个如下的 PDO 连接
$host = '127.0.0.1'; $db = 'database name'; $user = 'username'; $pass = 'password'; // this is very nice collation to use $charset = 'utf8mb4'; $dsn = "mysql:host={$host};dbname={$db};charset={$charset}"; $options = [ // add this option to show exception on any bad condition PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode()); }
然后创建点击计数器实例并使用其方法
$hitCounter = new HitCounter($pdo); // use hit methods $hitCounter->hitDaily('the_url'); // ...
HitCounter类
__construct(PDO $pdo_instance, ?array $config = null, bool $test_mode = false)
$config: 您的配置,如架构中所示。
请参阅 src/_Config 路径下的配置文件。
$test_mode: 默认情况下,不允许未知或无效的用户IP地址、爬虫和 DNT 标头进行点击。为了防止这种情况,发送参数值为 true。
runConfig()
为了使配置生效并创建表,请调用此方法。
hitDaily(string $url)
进行每日点击。
hitWeekly(string $url)
进行每周点击。
hitMonthly(string $url)
进行每月点击。
hitYearly(string $url)
进行每年点击。
hit(string $url, int $hit_at_times = IHitCounter::TIME_ALL)
根据第二个参数,为特定时间段进行点击。请参阅 常量 部分
注意:您可以使用多个常量与 | 运算符一起使用
report(?string $url, int $from_time, int $to_time, int $hit_with_types = self::TYPE_ALL): array
您可以从特定/所有URL从特定时间获取 views 和 unique_views 的数量。
freeReport(?string $url, string $where, array $bind_values = []): array
您可以从具有特定条件的特定/所有URL获取 views 和 unique_views 的数量。
注意:您应该使用命名参数,并将该参数的值放在 $bind_values 参数中。
saveDailyHits(string $path_to_store, bool $delete_from_database = false): bool
在今天的日期之前,将所有每日点击信息保存到文件中。
saveWeeklyHits(string $path_to_store, bool $delete_from_database = false): bool
在本周之前,将所有每周点击信息保存到文件中。
saveMonthlyHits(string $path_to_store, bool $delete_from_database = false): bool
在当前月份之前,将所有每月点击信息保存到文件中。
saveYearlyHits(string $path_to_store, bool $delete_from_database = false): bool
将当前年份之前的所有年度点击信息保存到文件中。
saveHits(string $path_to_store, int $hit_at_times = self::TIME_ALL, bool $delete_from_database = false): bool
将当前时间之前的所有点击信息保存到文件中。
注意:您可以使用多个常量与 | 运算符一起使用
常量
在 IHitCounter 接口类中存在一些常量,如下所示
时间常量:
-
TIME_DAILY
-
TIME_WEEKLY
-
TIME_MONTHLY
-
TIME_YEARLY
-
TIME_ALL
点击类型常量:
-
TYPE_DAILY
-
TYPE_WEEKLY
-
TYPE_MONTHLY
-
TYPE_YEARLY
-
TYPE_ALL
许可证
在 MIT 许可证下