lorddashme/php-simple-captcha
Composer 安装命令:
composer require lorddashme/php-simple-captcha
包简介
A simple captcha package that fit to any type of web application built on php.
README 文档
README
A simple captcha package that fit to any type of web application built on php.
Sample
Requirement(s)
- PHP version from 5.6.* up to latest.
Install
- Recommended to install using Composer. Use the command below to install the package:
composer require lorddashme/php-simple-captcha
Usage
List of Available Functions
| Function | Description |
|---|---|
code(length); |
Execute the generation of random code base on the given length. The default length is 5. |
image(); |
Execute the generation of image and the content will be base on the code(length); function. |
getCode(); |
To get the current code generated by the code(length); method. Ex. return value QwErTyx... |
getImage(); |
To get the current image generated by the image(); function. Ex. return value data:image/png;base64,iVBORw0KGgoAA... |
storeSession(); |
Use to store the generated values in the captcha session. |
getSession(); |
Use to get the current stored session generated values in the captcha session. This is use to validate the generated code against the user organic inputed code. Ex. return value array('code' => '...') |
- Basic usage:
<?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();
- Also can be done by using the code below:
<?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();
Captcha Session Usage
-
The package also provided a simple way to validate the user input code, for example we have a login feature.
-
Login Page:
-
Initialize the Captcha class together with the code and image generation process.
-
Use the
storeSession()to store the generated details in the captcha session. -
The stored session details are essential for validating the user input later on.
<?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>
-
-
Validation Route:
-
We need to initialize again the Captcha class but now we don't need to initialize the code and image generation.
-
Validate the user inputed captcha code.
<?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!'; }
-
-
You may also check the sample in the root directory of the package that will show you the actual example of implementing captcha class.
-
Captcha Configuration
- To change the default config of the class:
<?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);
Tips
-
Overriding the default config:
-
The
backgroundsandfontsare tightly coupled in the directory structure of the package. -
If you want to override the
backgroundsandfontsyou need to extends the Captcha class with your new sub class and override the protected methods of Captcha class for resources directory,backgroundsDirectoryPath()andfontsDirectoryPath.<?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/'; } }
-
License
This package is open-sourced software licensed under the MIT license.
lorddashme/php-simple-captcha 适用场景与选型建议
lorddashme/php-simple-captcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.97k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2018 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「security」 「github」 「Simple」 「captcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lorddashme/php-simple-captcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lorddashme/php-simple-captcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lorddashme/php-simple-captcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Handles basic OAuth/OAuth2 authentication along with classes for common services
Provide a way to secure accesses to all routes of an symfony application.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
It's a barebone security class written on PHP
Contao CMS integrity check for some files
Laravel package for the simplification and integration of many of your users most wanted login providers. Installation is a breeze. Never worry about OAuth again.
统计信息
- 总下载量: 2.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-11



