sokil / php-fraud-detect
检测请求洪泛
0.3
2015-12-02 21:49 UTC
Requires
- php: >=5.3
- sokil/php-list: 0.*
- symfony/event-dispatcher: 2.*
Requires (Dev)
- phpunit/phpunit: 3.7.*
- satooshi/php-coveralls: dev-master
This package is auto-updated.
Last update: 2024-08-28 00:53:21 UTC
README
欺诈请求检查器。该组件允许您检查某个用户是否没有超过允许的请求次数,并且如果请求次数达到限制,则执行一些任务,如封禁用户或显示验证码。了解更多关于 Token bucket 的信息。

安装
您可以通过 Composer 安装库
{ "require": { "sokil/php-fraud-detect": "dev-master" } }
基本用法
如果您的前端服务器功能不足,例如 Nginx 请求限制,并且您想自定义欺诈请求的检测,那么这个库就是为您准备的。
<?php $detector = new \Sokil\FraudDetector\Detector(); $detector // Configure unique user identifier like session id or track id or user ip. // This key defines scope of checking. It may limit check on concrete request, by session or globally // by user. So you can set key as concatenation of different parameters, e.g. $_SERVER['REQUEST_URE'] . session_id(). ->setKey(session_id()) // You can add few processors which execute different checks. // Processors may check request from proxy, existance of user in blacklist, etc. // This processor check if number of requests reached. ->declareProcessor('requestRate', function($processor, $detector) { /* @var $processor \Sokil\FraudDetector\Processor\RequestRateProcessor */ /* @var $detector \Sokil\FraudDetector\Processor\Detector */ $processor // Limit set as 5 requests for one second. // Collector used to store stat of requests ->setCollector($detector->createCollector( 'memcached', // collector type 'requestRate', // namespace 5, // requests 1, // time interval in seconds function($collector) { /* @var $collector \Sokil\FraudDetector\Collector\MemcachedCollector */ $memcached = new \Memcached(); $memcached->addServer('127.0.0.1', 11211); $collector->setStorage($memcached); } )); }) ->onCheckPassed(function() use($status) { // do something on success request }) ->onCheckFailed(function() use($status) { // do something if limits reached die('Request limits reached. Please, try again later'); }) ->check();
自定义处理器
您可以编写自己的处理器。它必须扩展 \Sokil\FraudDetector\AbstractProcessor
类。只需注册您处理器的命名空间并对其进行配置
<?php $detector = new \Sokil\FraudDetector\Detector(); $detector ->registerProcessorNamespace('\Acme\FraudDetecotor\Processor') ->declareProcessor('customProcessor', function($processor) {});
所有处理器将根据它们的注册优先级在其注册的命名空间中查找。