devlop / honeypot
为 Laravel FormRequest 制作的简单 honeypot
2.2.1
2021-03-05 07:53 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
Honeypot
为 Laravel FormRequest 制作的简单 honeypot,通过隐藏的 honeypot 字段检测垃圾邮件机器人。
安装
composer require devlop/honeypot
如果您希望更改 honeypot 的配置选项(例如组件名称),可以发布配置,但这通常不是必需的。
php artisan vendor:publish --provider="Devlop\Honeypot\HoneypotServiceProvider"
使用方法
首先,将 WithHoneypot
特性添加到您的 FormRequest 中。
namespace App\Http\Requests; use Devlop\Honeypot\WithHoneypot; use Illuminate\Foundation\Http\FormRequest; class DemoRequest extends FormRequest { use WithHoneypot;
然后您需要将 honeypot 添加到您的表单中。
<form method="POST" action="/"> <x-honeypot /> ... all your other form contents </form>
可选地,您还可以添加一个消息,当 honeypot 被触发时显示,这仅在自动验证时有效。
<form method="POST" action="/"> <x-honeypot> <p>Nice try! Go away!</p> </x-honeypot> ... all your other form contents </form>
最后,您需要配置验证,可以是自动的或手动的。
自动验证
将 honeypot 规则添加到您的规则配置中,这将使其在触发时重定向回表单,就像其他表单验证错误一样。
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() : array { return $this->withHoneypotRules([ // 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->getHoneypotInputName() => $this->honeypotRules(), ]; }
手动验证
如果您正在手动执行验证,您对如何处理垃圾邮件者有更多的控制,也许您想默默地忽略它,并给垃圾邮件者一种成功的印象?一切由您决定。
namespace App\Http\Controllers; use App\Requests\DemoRequest; use Illuminate\Http\Request; class DemoController { public function store(DemoRequest $request) { // get the honeypot $honeypot = $request->honeypot(); if ($honeypot->triggered()) { // do something when the honeypot was triggered } } }