https://github.com/devlop/honeypot
Simple Honeypot for Laravel FormRequests
https://github.com/devlop/honeypot
laravel spam-detection validation
Last synced: 15 days ago
JSON representation
Simple Honeypot for Laravel FormRequests
- Host: GitHub
- URL: https://github.com/devlop/honeypot
- Owner: devlop
- License: mit
- Created: 2021-02-04T09:33:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-05T07:54:17.000Z (about 5 years ago)
- Last Synced: 2024-12-01T05:42:20.375Z (over 1 year ago)
- Topics: laravel, spam-detection, validation
- Language: PHP
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Honeypot
Simple honeypot made for Laravel FormRequest that detects spam bots via a hidden honeypot field.
# Installation
```
composer require devlop/honeypot
```
If you wish to change any of the honeypot configuration options (such as the component name) you can publish the config, but this is usually not needed.
```
php artisan vendor:publish --provider="Devlop\Honeypot\HoneypotServiceProvider"
```
# Usage
First, add the `WithHoneypot` trait to your FormRequest.
```php
namespace App\Http\Requests;
use Devlop\Honeypot\WithHoneypot;
use Illuminate\Foundation\Http\FormRequest;
class DemoRequest extends FormRequest
{
use WithHoneypot;
```
Next you need to add the honeypot to your form.
```html
... all your other form contents
```
Optionally you can add a message to show when the honeypot was triggered, this only works when using automatic validation.
```html
Nice try! Go away!
... all your other form contents
```
Lastly, you need to configure the validation, it can either be automatic or manual.
## Automatic validation
Add the honeypot rules to your rules configuration, this will make it redirect back to the form when triggered, as any other form validation error.
```php
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() : array
{
return $this->withHoneypotRules([
// your normal rules goes here
]);
}
```
Optionally you can also register the rules like this
```php
/**
* 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(),
];
}
```
## Manual validation
If you are doing the validation manually you have more control of how you handle spammers,
maybe you want to silently ignore it and give the spammer the impression of success? it's all up to you.
```php
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
}
}
}
```