emotionloop/visualcaptcha
Composer 安装命令:
composer require emotionloop/visualcaptcha
包简介
PHP library for visualCaptcha. Still requires you to have the front-end companion.
README 文档
README
visualCaptcha-packagist
Packagist composer package for visualCaptcha's backend service
Installation with Composer
You need PHP installed with composer.
composer install emotionloop/visualcaptcha
Run tests
You need PHPUnit installed and then you can run
composer install && phpunit tests
Usage
Initialization
You have to initialize a session for visualCaptcha to inject the data it needs. You'll need this variable to start and verify visualCaptcha as well.
session_start();// Only needed once, at the beginning of your PHP files.
$session = new \visualCaptcha\Session( $namespace );
Where:
$namespaceis optional. It's a string and defaults to 'visualcaptcha'. You'll need to specifically set this if you're using more than one visualCaptcha instance in the same page, so the code can identify from which one is the validation coming from.
Using Cache
You can use a backend Zend-Cache library options to store images on cache and avoid I/O. You'll have to pass options parameter on the constructor as document in https://docs.zendframework.com/zend-cache/storage/adapter/. By default it is disabled and you´ll have to pass true on the constructor on the 5th parameter.
$options = array(
'adapter' => array(
'name' => 'memory',
'options' => array('ttl' => 3600,
'namespace' => "captcha-service"),
),
'plugins' => array(
'exception_handler' => array('throw_exceptions' => false),
),
);
$captchaWithCache = new \visualCaptcha\Captcha( $this->session,null,null,null, true,$options);
Setting Routes for the front-end
You also need to set routes for /start/:howmany, /image/:index, and /audio/:index. These will usually look like:
// Populates captcha data into session object
// -----------------------------------------------------------------------------
// @param howmany is required, the number of images to generate
$app->get( '/start/:howmany', function( $howMany ) use( $app ) {
$captcha = new \visualCaptcha\Captcha( $app->session );
$captcha->generate( $howMany );
$app->response[ 'Content-Type' ] = 'application/json';
echo json_encode( $captcha->getFrontEndData() );
} );
// Streams captcha images from disk
// -----------------------------------------------------------------------------
// @param index is required, the index of the image you wish to get
$app->get( '/image/:index', function( $index ) use( $app ) {
$captcha = new \visualCaptcha\Captcha( $app->session );
if ( ! $captcha->streamImage(
$app->response,
$index,
$app->request->params( 'retina' )
) ) {
$app->pass();
}
} );
// Streams captcha audio from disk
// -----------------------------------------------------------------------------
// @param type is optional and defaults to 'mp3', but can also be 'ogg'
$app->get( '/audio(/:type)', function( $type = 'mp3' ) use( $app ) {
$captcha = new \visualCaptcha\Captcha( $app->session );
if ( ! $captcha->streamAudio( $app->response, $type ) ) {
$app->pass();
}
} );
Validating the image/audio
Here's how it'll usually look:
$session = new \visualCaptcha\Session();
$captcha = new \visualCaptcha\Captcha( $session );
$frontendData = $captcha->getFrontendData();
// If an image field name was submitted, try to validate it
if ( $imageAnswer = $app->request->params( $frontendData[ 'imageFieldName' ] ) ) {
if ( $captcha->validateImage( $imageAnswer ) ) {
// Image was valid.
} else {
// Image was submitted, but wrong.
}
// Otherwise an audio field was submitted, so try to validate it
} else if ( $audioAnswer = $app->request->params( $frontendData[ 'audioFieldName' ] ) ) {
if ( $captcha->validateAudio( $audioAnswer ) ) {
// Audio answer was valid.
} else {
// Audio was submitted, but wrong.
}
} else {
// Apparently no fields were submitted, so the captcha wasn't filled.
}
visualCaptcha/Session properties
$namespace, String — This is private and will hold the namespace for each visualCaptcha instance. Defaults to 'visualcaptcha'.
visualCaptcha/Session methods
__construct( $namespace )— Initialize the visualCaptcha session.clear()— Will clear the session for the current namespace.get( $key )— Will return a value for the session's$key.set( $key, $value )— Set the$valuefor the session's$key.
visualCaptcha/Captcha properties
$session, Object that will have a reference for the session object. It will have .visualCaptcha.images, .visualCaptcha.audios, .visualCaptcha.validImageOption, and .visualCaptcha.validAudioOption.$assetsPath, Assets path. By default, it will be './assets'$imageOptions, All the image options. These can be easily overwritten or extended using addImageOptions( ), or replaceImageOptions( ). By default, they're populated using the ./images.json file$audioOptions, All the audio options. These can be easily overwritten or extended using addAudioOptions( ), or replaceAudioOptions( ). By default, they're populated using the ./audios.json file
visualCaptcha/Captcha methods
You'll find more documentation on the code itself, but here's the simple list for reference.
__construct( $session, $assetsPath = null, $defaultImages = null, $defaultAudios = null )— Initialize the visualCaptcha object.generate( $numberOfOptions = 5 )— Will generate a new valid option, within a$numberOfOptions.streamAudio( $headers, $fileType )— Stream audio file.streamImage( $headers, $index, $isRetina )— Stream image file given an index in the session visualCaptcha images array.getFrontendData()— Get data to be used by the frontend.getValidImageOption()— Get the current validImageOption.getValidAudioOption()— Get the current validAudioOption.validateImage( $sentOption )— Validate the sent image value with the validImageOption.validateAudio( $sentOption )— Validate the sent audio value with the validAudioOption.getImageOptions()— Return generated image options.getImageOptionAtIndex( $index )— Return generated image option at index.getAudioOption()— Alias for getValidAudioOption.getAllImageOptions()— Return all the image options.getAllAudioOptions()— Return all the audio options.
License
View the LICENSE file.
emotionloop/visualcaptcha 适用场景与选型建议
emotionloop/visualcaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124.94k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2014 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「captcha」 「visualCaptcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 emotionloop/visualcaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 emotionloop/visualcaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 emotionloop/visualcaptcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
Provide a way to secure accesses to all routes of an symfony application.
Two Captcha
It's a barebone security class written on PHP
Visual captcha implementation for yii2 framework
Laravel 5 Securimage helper
统计信息
- 总下载量: 124.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-01-30