
In web development, protecting your site from malicious traffic and spammers is crucial to maintaining performance and security. Laravel, a popular PHP framework, provides many ways to secure your application. One effective way to safeguard your Laravel site is by blocking known spam IP addresses. In this article, we will explore how to use the Laravel Abuse IP package to keep your application safe from spammers and harmful traffic.
The Laravel Abuse IP package is a security tool that allows developers to automatically block or filter spam IP addresses in their Laravel applications. It integrates with the AbuseIPDB database, which tracks malicious IP addresses globally. This package allows your Laravel site to automatically fetch and update the latest IP blacklists, protecting it from known spam sources.
composer require rahulalam31/laravel-abuse-ipphp artisan vendor:publish --tag=laravel-abuse-ipThis will allow you to customize the configuration to fit your application’s needs, including changing the storage path and enabling the optional ip2long() compression feature for IP addresses.
php artisan abuseip:updateHowever, the better practice is to schedule regular updates using Laravel’s built-in task scheduler. To set up a daily update, add the following to your routes/console.php file:
use Illuminate\Support\Facades\Schedule;
Schedule::command('abuseip:update')->daily();This ensures your application always has the most up-to-date list of spam IP addresses.
For Laravel 10 and below, add the middleware in the Http/Kernel.php file:
protected $middleware = [
\RahulAlam31\LaravelAbuseIp\Middleware\AbuseIp::class,
];For Laravel 11 and newer, you can register the middleware in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->append(\RahulAlam31\LaravelAbuseIp\Middleware\AbuseIp::class);
})Kernel.php:protected $routeMiddleware = [
'abuseip' => \RahulAlam31\LaravelAbuseIp\Middleware\AbuseIp::class,
];Then, use the middleware in your routes:
Route::get('/xyz', function () {
// Your route logic
})->middleware('abuseip');This allows you to protect specific endpoints from malicious users or traffic.
config/abuseip.php file.