定制 tuantq/laravel-prevent-spam 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tuantq/laravel-prevent-spam

最新稳定版本:v1.0.0

Composer 安装命令:

composer require tuantq/laravel-prevent-spam

包简介

Prevent spam with honeypot field

README 文档

README

A lightweight Laravel package to protect your forms from spam bots using a honeypot and timing protection.
Simple to use, framework-native, and customizable.

🚀 Features

  • 🧠 Invisible honeypot field to trap spam bots.
  • ⏱️ Time-based protection: detects form submissions that are too fast.
  • ⚙️ Fully configurable via config/honeypot.php.
  • 💡 Blade component <x-prevent-spam::honeypot /> for easy integration.
  • 🔒 Middleware-ready — just add it to any form processing route.
  • 🧩 Easily override exceptions for custom responses.

🧩 Installation

Install the package via Composer:

composer require tuantq/laravel-prevent-spam

If you're using Laravel 10+ or 11+, package discovery will automatically register the service provider and config.

For older Laravel versions, register manually in config/app.php:

'providers' => [
    Tuantq\LaravelPreventSpam\PreventSpamServiceProvider::class,
],

⚙️ Publishing Configuration & Views

After installation, you can publish the config and view files to customize them:

php artisan vendor:publish --provider="Tuantq\LaravelPreventSpam\PreventSpamServiceProvider" --tag=config
php artisan vendor:publish --provider="Tuantq\LaravelPreventSpam\PreventSpamServiceProvider" --tag=views

This will create a file at: config/honeypot.php This will copy the package view to: resources/views/vendor/prevent-spam/honeypot.blade.php You can then edit this file freely to match your form structure or CSS style.

🧱 Usage

1️⃣ Add honeypot field to your form

In your Blade form:

<form method="POST" action="{{ route('post.store') }}">
    @csrf
    <x-prevent-spam::honeypot />

    <!-- your form inputs -->
    <input type="text" name="title">
    <button type="submit">Submit</button>
</form>

This component automatically renders:

<div style="display: none" aria-hidden="true">
    <input id="my_name" name="my_name" type="text" value="" autocomplete="nope" tabindex="-1">
    <input id="my_time" name="my_time" type="text" value="1761397891.6765" autocomplete="off" tabindex="-1">
</div>

2️⃣ Protect your route or controller

Use the provided middleware to check the honeypot and timing:

use Tuantq\LaravelPreventSpam\Middleware\PreventSpam;

Route::post('/post/create', [PostController::class, 'store'])
    ->middleware(PreventSpam::class)
    ->name('posts.store');

3️⃣ Custom exception handling

You can customize the spam handling logic globally or per middleware.

Option 1: Globally using Service Provider

// app/Providers/AppServiceProvider.php

use Tuantq\LaravelPreventSpam\Honeypot;

public function boot(): void
{
    Honeypot::abortUsing(function () {
        return response('Custom response for spam', 422);
    });
}

Option 2: Override in your own middleware

<?php
// app/Http/Middleware/BlockSpam.php

namespace App\Http\Middleware;

use Tuantq\LaravelPreventSpam\Middleware\PreventSpam;

class BlockSpam extends PreventSpam
{
    public function abort()
    {
        return response('Custom response for spam', 422);
    }
}

Then register your middleware instead of the default one.

⚙️ Configuration

Publish the config file:

php artisan vendor:publish --tag=config

Example config/honeypot.php:

return [
    'enabled' => env('HONEYPOT_ENABLED', true),
    'field_name' => env('HONEYPOT_NAME', 'my_name'),
    'field_time_name' => env('HONEYPOT_TIME', 'my_time'),
    'seconds' => env('HONEYPOT_SECONDS', 3),
];
Key Description Default
enabled Enable or disable honeypot protection true
field_name Name of hidden honeypot field my_name
field_time_name Hidden field to track form generation time my_time
seconds Minimum seconds before form can be submitted 3

🧪 Testing

You can test your form by submitting it immediately — you should see the 422 custom response. Wait more than the configured seconds, and it will pass normally.

💬 Credits

Developed by Tuấn TQ

🧾 License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-25