1. Go to this page and download the library: Download spatie/laravel-honeypot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / laravel-honeypot example snippets
use Spatie\Honeypot\SpamResponder\BlankPageResponder;
return [
/*
* Here you can specify name of the honeypot field. Any requests that submit a non-empty
* value for this name will be discarded. Make sure this name does not
* collide with a form field that is actually used.
*/
'name_field_name' => env('HONEYPOT_NAME', 'my_name'),
/*
* When this is activated there will be a random string added
* to the name_field_name. This improves the
* protection against bots.
*/
'randomize_name_field_name' => env('HONEYPOT_RANDOMIZE', true),
/*
* When this is activated, requests will be checked if
* form is submitted faster than this amount of seconds
*/
'valid_from_timestamp' => env('HONEYPOT_VALID_FROM_TIMESTAMP', true),
/*
* This field contains the name of a form field that will be used to verify
* if the form wasn't submitted too quickly. Make sure this name does not
* collide with a form field that is actually used.
*/
'valid_from_field_name' => env('HONEYPOT_VALID_FROM', 'valid_from'),
/*
* If the form is submitted faster than this amount of seconds
* the form submission will be considered invalid.
*/
'amount_of_seconds' => env('HONEYPOT_SECONDS', 1),
/*
* This class is responsible for sending a response to requests that
* are detected as being spammy. By default a blank page is shown.
*
* A valid responder is any class that implements
* `Spatie\Honeypot\SpamResponder\SpamResponder`
*/
'respond_to_spam_with' => BlankPageResponder::class,
/*
* This class is responsible for applying all protection
* rules for a request. By default uses `request()`.
*
* It throws the `Spatie\Honeypot\ExceptionsSpamException` if the
* request is flagged as spam, or returns void if it succeeds.
*/
'spam_protection' => \Spatie\Honeypot\SpamProtection::class,
/*
* When activated, requests will be checked if honeypot fields are missing,
* if so the request will be stamped as spam. Be careful! When using the
* global middleware be sure to add honeypot fields to each form.
*/
'honeypot_fields_
use App\Http\Controllers\ContactFormSubmissionController;
use Spatie\Honeypot\ProtectAgainstSpam;
Route::post('contact', [ContactFormSubmissionController::class, 'create'])->middleware(ProtectAgainstSpam::class);
use Spatie\Honeypot\ProtectAgainstSpam;
Route::middleware(ProtectAgainstSpam::class)->group(function() {
Auth::routes();
});
// in a controller
public function create(\Spatie\Honeypot\Honeypot $honeypot)
{
return inertia('contactform.show', [
'honeypot' => $honeypot,
]);
}
use Spatie\Honeypot\Http\Livewire\Concerns\UsesSpamProtection;
class YourComponent extends Component
{
use UsesSpamProtection;
use Spatie\Honeypot\Http\Livewire\Concerns\HoneypotData;
class YourComponent extends Component
{
// ...
public HoneypotData $extraFields;
public function mount()
{
$this->extraFields = new HoneypotData();
}
public function submit(): void
{
$this->protectAgainstSpam(); // if is spam, will abort the request
User::create($request->all());
}
}
use App\Models\User;
use Spatie\Honeypot\Http\Livewire\Concerns\HoneypotData;
use Spatie\Honeypot\Http\Livewire\Concerns\UsesSpamProtection;
use function Livewire\Volt\{uses, state, mount};
uses(UsesSpamProtection::class);
state([
// ...
'extraFields' => null,
]);
mount(function () {
$this->extraFields = new HoneypotData();
});
$guessHoneypotDataProperty = fn () => $this->extraFields;
$submit = function () {
$this->protectAgainstSpam(); // if is spam, will abort the request
User::create($request->all());
};
config()->set('honeypot.enabled', false)
namespace Spatie\Honeypot\SpamResponder;
use Closure;
use Illuminate\Http\Request;
interface SpamResponder
{
public function respond(Request $request, Closure $next);
}
// in your spam responder
$regularResponse = $next($request)