diszo2009 / multi-captcha
一包内包含多种(7种)验证码类型。支持Gif验证码(动画Gif验证码)、图片验证码(用户必须输入图像中显示的某些或全部代码)、陷阱验证码(简单地添加一个空白字段要求用户留空。机器人会填满它)、ASCII验证码(显示
Requires
- php: >=5.3.0
- google/recaptcha: ~1.1
This package is auto-updated.
Last update: 2024-09-12 21:23:55 UTC
README
正如其名,这是一个包,可以渲染所有验证码!一包内包含多种验证码类型。有时我们只需要避免垃圾邮件机器人,同时仍然保持表单对用户友好,有时我们需要完全保护,即使这可能会让用户感到有些复杂。MultiCaptcha可以帮助您在项目上实现统一的验证码验证代码。
主要功能
- 支持7种验证码类型,并为每种类型提供多种配置选项。
- 显示刷新按钮(您需要提供刷新URL)。
- 每个验证码挑战只能提交一次。
- 可自定义错误消息
- 您可以使用 themeOptions 来自定义外观和感觉,或者您可以编写自己的主题。
支持的验证码类型
安装
使用Composer
命令行
您可以使用Composer安装MultiCaptcha,执行以下操作
composer require sameer-shelavale/multi-captcha
composer.json
或者,您可以直接在您的 composer.json 文件中的 require 块中添加它
{
"require": {
"sameer-shelavale/multi-captcha": "1.3.*"
}
}
然后运行
composer update
PHP 包含
或者,您可以下载zip/rar存档,将其解压,并将/src文件夹复制到您的项目文件夹中的适当位置。然后包含captcha.php
注意:请确保缓存目录可写。
include_once( 'PATH-TO-MULTI-CAPTCHA-FOLDER/src/Captcha.php' );
用法
初始化
初始化验证码所需的最小代码如下所示
$captcha = new \MultiCaptcha\Captcha([ 'secret'=> "your-secret-key", ] );
上述代码将初始化验证码对象以输出“图片”验证码,这是默认值。注意:设置您自己的密钥非常重要,因为它用于加密和解密验证码字段。
更详细的代码如下所示
$captcha = new \MultiCaptcha\Captcha([ 'secret'=> "your-secret-key", 'life' => 2, //number of hours the generated captcha will be valid 'customFieldName' => 'my_captcha', //this becomes the name of captcha answer field 'options' => [ 'image' => [ 'maxCodeLength' => 8, 'font'=>'../src/types/image/comic.ttf', 'width'=>180 ] ] ] );
现在,我们了解了如何初始化它,让我们详细看看支持的参数。
支持的参数
选项
目前我们可以渲染7种验证码,即 image、gif、ascii、math、honeypot、recaptcha 和 nocaptcha。现在让我们详细看看每种验证码支持的配置参数。
图片验证码
Gif验证码(GIF动画验证码)
ASCII验证码
ASCII字体名称:您可以在 src/types/ascii/fonts/ 文件夹中找到当前支持的ASCII figlet字体,并使用不带.flf扩展名的字体名称。
ASCII验证码配置示例
$captcha = new \MultiCaptcha\Captcha([ 'secret'=> "your-secret-key", 'options' => [ 'ascii' => [ 'maxCodeLength' => 8, 'fonts'=>array( 'banner' => 4, //render with font size 4px or it becomes too big 'doom' => 8, //render with font size 8px 'small' =>'8' //render with font size 8px, "small" font is at src/types/ascii/fonts/small.flf ) ] ] ] );
注意,fonts参数,它以无扩展名的字体名称为键,像素大小为值。除非您提供 fontPath 参数,否则它将在 src/types/ascii/fonts/ 文件夹中查找该字体。
数学验证码(简单的数学表达式)
陷阱验证码
Recaptcha
注意:您可以在 http://www.google.com/recaptcha 注册并获取您的recaptcha密钥。
无验证码
您可以从配置的多种类型中生成随机的验证码。
例如,您可以做
$captcha = new \MultiCaptcha\Captcha([ 'secret'=> "your-secret-key", 'options' => [ 'math' => array( 'description'=> "Answer following question if you are human", 'level' => 4 ), 'image' => array( 'maxCodeLength' => 8, 'font'=>'../src/types/image/comic.ttf', 'width'=>180 ), 'ascii' => array( 'maxCodeLength' => 8, 'fonts'=>array( 'banner'=> 4, 'doom'=> 8, 'small'=>'8' ) ), 'gif' => array( 'maxCodeLength' => 8, 'font'=>'../src/types/image/comic.ttf', 'width'=>180, 'height'=>60, 'totalFrames'=>50, 'delay'=>20 ) ] ] );
然后它将生成一个随机类型的验证码,该验证码来自配置的4种类型
渲染
您可以使用以下单行代码渲染验证码
echo $captcha->render() ;
这就完成了。(注意$captcha是您初始化的对象的名称)
刷新
要显示刷新按钮,必须提供refreshUrl。然后在该URL的脚本中,您可以这样做。
echo $captcha->refresh() ; exit; //this is important to ensure no html is trailing the captcha
注意:确保在 captcha->refresh();
前后没有显示 HTML。您也可以在同一页面上渲染和刷新,请参考 gif、ascii、图像和数学 captcha 示例。
验证
您可以通过以下方式简单地验证表单数据
if( $captcha->validate( $_POST ){ //do further processing, validate individual form fields }
注意:我们需要将所有提交的数据传递给验证函数,因为 captcha 字段的名称是加密且随机的。如果您指定了 customFieldName 参数,则需要该字段和挑战字段进行验证。例如,如果 customFieldName = my_captcha,那么您需要传递一个数组
if( $captcha->validate( [ 'my_captcha'=>$_POST['my_captcha'], 'my_captcha_challenge'=>$_POST['my_captcha_challenge'] ] ){ //do further processing, validate individual form fields }
或
if( $captcha->validate( array_intersect_key($_POST, array_flip(['my_captcha', 'my_captcha_challenge']) ) ) ){ //do further processing, validate individual form fields }
主题/定制
MultiCaptcha 附带一个名为 DefaultTheme 的默认主题。此主题支持通过 themeOptions 下面的各种参数定制其颜色/样式。例如,您可以将背景颜色更改为蓝色
$captcha = new \MultiCaptcha\Captcha([
'secret'=> "form1-secret-key",
'options' => [
'image' => array(
'maxCodeLength' => 8,
'font'=>'../src/types/image/comic.ttf',
'width'=>180,
'themeOptions' => [
'containerStyle' => 'border:1px solid #0f702d; border-radius: 5px; padding: 5px; display: table; margin: 2px; background-color: #29713f; font-family: Helvetica; font-size: 14px; max-width: 180px;position:relative;',
'fieldStyle' => 'background-color:#52785e; border:2px solid #fff; color:#fff; font-size:120%; font-weight:bold; border-radius:3px;width:144px;',
'labelStyle' => 'font-size:80%; line-height:100%; color: #fff;',
]
),
'ascii' => array(
'maxCodeLength' => 8,
'fonts'=>array(
'banner'=> 4,
'doom'=> 8,
'small'=>'8'
),
'themeOptions' => [
'containerStyle' => 'border:1px solid #1e2a37; border-radius: 5px; padding: 10px; display: table; margin: 2px; background-color: #374c63; font-family: Helvetica; font-size: 14px; max-width: 180px;position:relative;',
'fieldStyle' => 'background-color:#4d5d6f; border:2px solid #fff; color:#fff; font-size:120%; font-weight:bold; border-radius:3px;width:144px;',
'labelStyle' => 'font-size:80%; line-height:100%; color: #fff;'
]
),
],
'refreshUrl'=>'random.php?captcha=refresh',
'helpUrl'=>'http://github.com/sameer-shelavale/multi-captcha',
] );
注意:每种 captcha 类型都可以有自己的 theme 和 themeOptions
DefaultTheme 的 themeOptions
使用上述 themeOptions,您将能够更改默认主题的外观和感觉。但是,如果您需要更改元素的位置,您将必须通过扩展 DefaultTheme 编写自己的主题。请参阅 example/theming.php 以获取 themeOptions 的有效示例。
扩展 DefaultTheme
- render() 和 refresh() 函数对于渲染 captcha 至关重要,您的主题必须包含它们。
- 当您扩展 DefaultTheme 时,您可以通过设置 theme 使用该主题,例如。
$captcha = new \MultiCaptcha\Captcha([
'secret'=> "form1-secret-key",
'options' => [
'image' => array(
'maxCodeLength' => 8,
'font'=>'../src/types/image/comic.ttf',
'width'=>180,
'theme' => 'CustomTheme1'
),
'ascii' => array(
'maxCodeLength' => 8,
'fonts'=>array(
'banner'=> 4,
'doom'=> 8,
'small'=>'8'
),
'theme' => 'CustomTheme2'
),
],
] );
注意:当您创建自己的主题时,您可以将自己的 themeOptions。同时,请记住更新 javascipt 刷新函数,每次更改结构/布局时都需要更新。
缓存
Multicaptcha 使用文件缓存来记录已回答的 captcha 并使用它来阻止使用单个 captcha 和多个答案的暴力攻击。它将 captcha 的唯一 ID 和过期时间存储在记录中,并且该记录将保留到 captcha 过期。它使用文件缓存来存储这些记录。为了避免缓存太大,记录会分散到多个文件中。用于缓存的文件数量由变量 $cacheSize 指定,而文件应存储的目录由 $cacheDir 指定,您可以将这两个变量作为参数传递给构造函数。默认缓存大小为 10,但对于繁忙的网站,您可以增加它。注意:如果您正在使用默认的文件缓存实现,则缓存目录必须可写。
进行中的功能
- 多语言支持
计划中的功能
- 为图像和 gif captcha 定制背景图像
许可证
AGPL3.0,非商业用途免费。有关其他类型许可,请发邮件至 samiirds@gmail.com。