mistralys/errorcode-incrementor

一个简单的PHP脚本,通过HTTP请求或程序性地增加计数器。

1.0.1 2021-03-19 14:50 UTC

This package is auto-updated.

Last update: 2024-09-19 22:41:30 UTC


README

一个简单的PHP脚本,当访问时增加计数器,并显示当前计数。

当作为项目的composer依赖项使用时,也可以程序性地使用。

安装

  • 在您的Web服务器根目录下选择一个文件夹进行检出
  • config-local.dist.php 重命名为 config-local.php
  • 确保 storage 文件夹可被脚本写入
  • 编辑文件中的所需设置,设置您希望使用的计数器(参见配置)

配置

密码

为了防止在DOS攻击中占用服务器,计数器被密码保护,因为它们会在硬盘上执行写操作,从而在这样的环境中创建大量的系统负载。

尽管这似乎是一个微不足道的操作,但请务必选择一个好的密码。

计数器列表

config-local.php 中的计数器数组非常简单。假设您有两个应用程序,您想要维护错误代码计数器,分别称为“Mistral”和“Tramontane”(风的名字)。计数器列表将如下所示

<?php
$counters = array(
    'Mistral' => 0,
    'Tramontane' => 520 // start at #520
);

注意:对名称中使用的字符没有限制 - 您可以安全地使用UTF8和特殊字符。

更新延迟

参见安全 > 最小更新延迟。

快速开始

要开始检查计数器值,将您的浏览器指向脚本所在位置,并使用以下GET参数

  • 查看计数器: /path/?pw=****&counter=(name)
  • 增加计数器: /path/?pw=****&counter=(name)&increment=yes

两者都将显示所选计数器的当前值。

安全

最小更新延迟

为了减轻DOS攻击或避免对计数器进行垃圾邮件攻击,在增加计数器之间强制执行更新延迟。默认情况下,每10秒只能增加一次计数器。

绝对最小延迟为1秒。

注意:延迟针对每个计数器单独执行,仅在增加时使用。

HTTPS推荐

由于计数器使用密码,因此不建议通过HTTP使用它,因为相对容易拦截请求中的密码。使用带有SSL证书的Web服务器,并使用HTTPS连接提供脚本。

错误处理

当发生错误时,脚本将发送带有描述性状态文本的HTTP状态码。

可能的错误

  • 401 错误或无密码
  • 400 未指定计数器名称
  • 404 指定的计数器不存在
  • 403 不遵守最小更新延迟
  • 500 计数器数据无法保存/加载

程序性地访问计数器

可以在不通过HTTP请求提供计数器服务的情况下使用 Counters 类。这意味着您可以将此包作为项目的依赖项,并在那里使用 Counters 类。

获取计数器值

<?php
// The list of counters
$counters = array(
    'Test' => 0
);

// Create the collection with the path to the folder
// where the files can be stored - must be writable.
$collection = new \Mistralys\Counters\Counters('./storage', $counters);

// To avoid an exception, always check first
if($collection->counterExists('Test'))
{
    $number = $collection->getByName('Test')->getNumber();
}  

增加计数器

<?php
// The list of counters
$counters = array(
    'Test' => 0
);

// Create the collection with the path to the folder
// where the files can be stored - must be writable.
$collection = new \Mistralys\Counters\Counters('./storage', $counters);

// To avoid an exception, always check first
if($collection->counterExists('Test'))
{
    // Incrementing returns the new counter value
    $newNumber = $collection->getByName('Test')->increment();
}  

检索可用计数器

<?php
// The list of counters
$counters = array(
    'Test' => 0
);

// Create the collection with the path to the folder
// where the files can be stored - must be writable.
$collection = new \Mistralys\Counters\Counters('./storage', $counters);

$counters = $collection->getCounters();

// Display a list of counters and their values
foreach($counters as $counter)
{
    echo $counter->getName().' = '.$counter->getNumber().PHP_EOL;
}