devlop / speedtrap
为Laravel FormRequest设计的简单speedtrap蜜罐
1.2.2
2021-03-05 08:38 UTC
Requires
- php: ^7.4|^8.0
- illuminate/collections: ^7.0|^8.0
- illuminate/container: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- laravel/framework: ^7.0|^8.0
README
Speedtrap
为Laravel FormRequest设计的简单speedtrap蜜罐,通过测量提交表单所需的时间来检测垃圾邮件机器人。
安装
composer require devlop/speedtrap
如果您想更改任何speedtrap配置选项(如默认阈值5秒或组件名称),可以发布配置,但这通常不是必需的。
php artisan vendor:publish --provider="Devlop\Speedtrap\SpeedtrapServiceProvider"
用法
首先,将WithSpeedtrap
特性添加到您的FormRequest中。
namespace App\Http\Requests; use Devlop\Speedtrap\WithSpeedtrap; use Illuminate\Foundation\Http\FormRequest; class DemoRequest extends FormRequest { use WithSpeedtrap;
接下来,您需要将speedtrap添加到您的表单中。
<form method="POST" action="/"> <x-speedtrap /> ... all your other form contents </form>
可选地,您可以在speedtrap触发时显示一条消息,这仅在使用自动验证时有效。
<form method="POST" action="/"> <x-speedtrap> <p>Slow down there muchacho!</p> </x-speedtrap> ... all your other form contents </form>
最后,您需要配置验证,可以是自动的或手动的。
自动验证
将speedtrap规则添加到您的规则配置中,这将使其在触发时重定向回表单,就像任何其他表单验证错误一样。
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() : array { return $this->withSpeedtrapRules([ // your normal rules goes here ]); }
可选地,您也可以这样注册规则
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() : array { return [ // your normal rules goes here, $this->getSpeedtrapInputName() => $this->speedtrapRules(), ]; }
手动验证
如果您手动进行验证,您将有更多控制如何处理垃圾邮件发送者,也许您想默默地忽略它并给垃圾邮件发送者一种成功的印象?这完全取决于您。
namespace App\Http\Controllers; use App\Requests\DemoRequest; use Illuminate\Http\Request; class DemoController { public function store(DemoRequest $request) { // get the speedtrap $speedtrap = $request->speedtrap(); if ($speedtrap->triggered()) { // do something when the speedtrap was triggered } } }