lorddashme / php-simple-captcha
一个简单的验证码包,适用于任何基于php构建的Web应用程序。
1.2.0
2018-10-01 08:15 UTC
Requires
- php: >=5.6 || >=7.0 || >=7.1 || >=7.2
- lorddashme/php-static-class-interface: 1.*
Requires (Dev)
- mockery/mockery: 1.*
- phpunit/phpunit: 5.* || 6.* || 7.*
This package is auto-updated.
Last update: 2024-09-30 01:26:49 UTC
README
一个简单的验证码包,适用于任何基于php构建的Web应用程序。
示例
要求
- PHP版本从5.6.*到最新版本。
安装
- 建议使用Composer进行安装。使用以下命令安装该包:
composer require lorddashme/php-simple-captcha
用法
可用函数列表
- 基本用法
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; // Initialize the captcha class. $captcha = new Captcha(); // Execute the random generation of code. $captcha->code(); // Execute the image captcha rendering. $captcha->image(); // The generated captcha code, something like "QwErTyx..." echo $captcha->getCode(); // The generated captcha image that include the code above. // The output is base64 data image "data:image/png;base64,iVBORw0KGgoAA..." echo $captcha->getImage();
- 也可以使用以下代码实现
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Facade\Captcha; // Initialize the facade captcha class. Captcha::init(); // Execute the random generation of code. Captcha::code(); // Execute the image captcha rendering. Captcha::image(); // The generated captcha code, something like "QwErTyx..." echo Captcha::getCode(); // The generated captcha image that include the code above. // The output is base64 data image "data:image/png;base64,iVBORw0KGgoAA..." echo Captcha::getImage();
验证码会话用法
-
该包还提供了一个简单的方式来验证用户输入的代码,例如我们有一个登录功能。
-
登录页面
-
初始化验证码类以及代码和图像生成过程。
-
使用
storeSession()
将生成的详细信息存储在验证码会话中。 -
存储的会话详细信息对于稍后验证用户输入至关重要。
<?php // login.php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; $captcha = new Captcha(); $captcha->code(); $captcha->image(); $captcha->storeSession(); ?> <form action="validate-login.php" method="POST"> ... <img src="<?php echo $captcha->getImage(); ?>"> <input type="text" name="user_captcha_code" value=""> <input type="submit" value="Login"> </form>
-
-
验证路由
-
我们需要再次初始化验证码类,但现在我们不需要初始化代码和图像生成。
-
验证用户输入的验证码代码。
<?php // validate-login.php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; $captcha = new Captcha(); $data = $captcha->getSession(); // return array( 'code' => 'QwErTyx...' ) if ($_POST['user_captcha_code'] === $data['code']) { return 'Code is valid!'; } else { return 'Code is invalid!'; }
-
-
您还可以查看包根目录中的示例,它将展示实现验证码类的实际示例。
-
验证码配置
- 要更改类的默认配置
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; use LordDashMe\SimpleCaptcha\Facade\Captcha as CaptchaFacade; $config = array( 'session_name' => 'ldm-simple-captcha', 'session_index_name' => 'LDM_SIMPLE_CAPTCHA', 'session_https' => false, 'session_http_only' => true, 'font_color' => '#000', 'font_size_min' => 26, 'font_size_max' => 28, 'angle_min' => 0, 'angle_max' => 9, 'shadow' => true, 'shadow_color' => '#fff', 'shadow_offset_x' => -3, 'shadow_offset_y' => 1, 'backgrounds' => array( 'bg1.png', 'bg2.png', 'bg3.png', 'bg4.png', 'bg5.png', 'bg6.png', 'bg7.png', 'bg8.png' ), 'fonts' => array( 'capsmall_clean.ttf' ) ); $captcha = new Captcha($config); // Or you can use this style. CaptchaFacade::init($config);
提示
-
覆盖默认配置
-
在包的目录结构中,
backgrounds
和fonts
紧密耦合。 -
如果您想覆盖
backgrounds
和fonts
,您需要使用您的新子类扩展验证码类,并覆盖验证码类中用于资源目录的保护方法backgroundsDirectoryPath()
和fontsDirectoryPath
。<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\SimpleCaptcha\Captcha; class MyNewCaptcha extends Captcha { public function __construct($config = array()) { parent::__construct($config); } protected function backgroundsDirectoryPath() { return 'path/to/your/custom/backgrounds/'; } protected function fontsDirectoryPath() { return 'path/to/your/custom/fonts/'; } }
-
许可协议
此包是开源软件,许可协议为MIT许可。