enovision/slim-captcha

Slim Captcha 是基于 CodeIgniter 的 captcha 辅助工具进行改编的

0.2.2 2018-03-15 07:43 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:31:29 UTC


README

这个易于使用的 captcha 包基于 CodeIgniter (Ellislab) 的 captcha 辅助工具。

安装

使用 Composer 将 Slim Captcha 安装到您的项目中

composer require enovision/slim-captcha

或从 github 克隆它

git clone https://github.com/enovision/slim-captcha

要求

  • akrabat/rka-ip-address-middleware

与 Slim 3 一起使用

要求

  • 'gd' 扩展(gdlib)必须被加载(用于创建图像)

  • 作为设置,必须将 img_path 设置为公共文件夹中的一个文件夹,例如 captcha(此文件夹必须是可写的(chmod 775))

    例如:$_SERVER['DOCUMENT_ROOT'] . '/captcha/'(作为 public_html/captcha 或 private_html/.. 中的文件夹)

  • 必须在设置中设置 img_url

    例如:'http(s)://' . $_SERVER['SERVER_NAME'] . '/captcha/'

数据库表 'captcha'

以下表是使此包成功所需的。以下提供 MySQL 代码,但您也可以实现自己的需求。所有数据库活动都是通过回调函数完成的,因此您也可以使用自己的实现。

CREATE TABLE `captcha` (
  `captcha_id` BIGINT(13) UNSIGNED NOT NULL AUTO_INCREMENT,
  `captcha_time` INT(10) NOT NULL,
  `ip_address` VARCHAR(45) NOT NULL,
  `word` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`captcha_id`),
  INDEX `word` (`word`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=0;

在 Slim 3 中使用 Eloquent ORM 的示例回调,如设置文件中所示

<?php
use Illuminate\Database\Capsule\Manager as DB;

$settings_captcha = [
    ... all the other settings ...,
    'callbackSave' => function ($captcha) {
        DB::table('captcha')->insert([
            'captcha_time' => $captcha['time'],
            'ip_address' => $captcha['ip_address'],
            'word' => $captcha['word']
        ]);
    },
    'callbackCleanUp' => function ($expiration) {
        DB::table('captcha')
          ->where('captcha_time', '<', $expiration)
          ->delete();
    },
    'callbackValidate' => function ($word, $ip_address, $expiration) {
        $count = DB::table('captcha')
          ->where('word', $word)
          ->where('ip_address', $ip_address)
          ->where('captcha_time', '>', $expiration)
          ->count();

        return $count;
    }
];

return $settings_captcha;

设置

Captcha 对象的设置可以从对象的默认值、设置文件或创建对象时提供。

重要性顺序

  • 在创建 captcha 对象时覆盖设置
  • /config/captcha/settings.php 中的 settings.php
  • 此类中的默认值

settings.php 文件的默认位置是 <same-folder-as-app-is>/config/captcha/settings.php。在 composer 包的根目录中可以找到一个示例 settings.php 文件,您可以将它复制到您首选的位置(而不是刚才提到的位置)。

在创建 captcha 时,您可以在对象创建时添加替代位置

$captcha = new \Enovision\Slim\Captcha($this, $request, $response, [
    'settings_path' => INC_ROOT . '/alternative/location/settings.php',
]);

$captcha();

创建 captcha

创建 captcha 非常简单

$captcha = new \Enovision\Slim\Captcha($this, $request, $response);
$captcha();

就是这样。它将在 html 'img' 标签中输出 captcha 代码。

如果您不想直接在客户端显示上输出,您也可以执行

$captcha = new \Enovision\Slim\Captcha($this, $request, $response);
$captcha = $captcha(false);

这将返回以下格式的数组

[
  'word' => generated word in the image,
  'time' => time of creation of the captcha,
  'image' => the image itself, including the html 'img' tag,
  'url' => url of the image,
  'filename' => filename of the generated image,
  'ip_address' => ip address of the client
]

在创建图像时,它还会在您的数据库中的 'captcha' 表中保存一条记录。

验证 captcha

$captcha = new \Enovision\Slim\Captcha($this, $request, $response);
$valid = $captcha->validateCaptcha($typed_in_word_from_a_form);

这将检查与数据库表 'captcha' 中的有效记录的有效性。它返回 truefalse

清理过期的 captcha

每次验证 captcha 时,都会清理已过期的旧记录。验证保持活跃的时间与 '过期' 设置(以秒为单位)相同。

字体

在这个包的 /assets/fonts 文件夹中,您将找到一个用于 captcha 图像的好看字体。如果您想使用它,将其复制到您网站/应用程序的公共部分的 assets 文件夹中。然后调整设置文件中的以下设置

'font_path' => $_SERVER['DOCUMENT_ROOT'] . '/somewhere/in/public/fonts/00118_20thCenturyFontBold.ttf',

致谢

Ellislab,为在 CodeIgniter 3 中创建此功能。

CodeIgniter,不列颠哥伦比亚理工学院,CodeIgniter