PHP code example of jlrodriguezdev / laravel-bot-protection

1. Go to this page and download the library: Download jlrodriguezdev/laravel-bot-protection 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/ */

    

jlrodriguezdev / laravel-bot-protection example snippets


protected $middlewareGroups = [
    'web' => [
        // ...
        \Jlrodriguezdev\BotProtection\Middleware\BotProtection::class,
    ],
];

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Jlrodriguezdev\BotProtection\Middleware\BotProtection::class,
    ]);
})

return [
    'enabled' => env('BOT_PROTECTION_ENABLED', true),
    'rate_limit' => env('BOT_PROTECTION_RATE_LIMIT', 60),
    'strike_threshold' => 3,
    'whitelist_ips' => [
        '127.0.0.1',
        '::1',
        // 'your.server.public.ip', // Whitelist server's own IP so internal CLI/cron calls are never blocked
    ],
    'blacklisted_ips' => [
        //
    ],
];
bash
php artisan vendor:publish --provider="Jlrodriguezdev\BotProtection\BotProtectionServiceProvider"
bash
> php artisan tinker --execute="
> \$req = \Illuminate\Http\Request::create('/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'curl/8.4.0', 'REMOTE_ADDR' => '1.2.3.4']);
> \$res = app(\Jlrodriguezdev\BotProtection\Middleware\BotProtection::class)->handle(\$req, fn(\$r) => 'OK');
> echo 'Status: ' . (is_string(\$res) ? \$res : \$res->getStatusCode()) . PHP_EOL;
> "
> # Output: Status: 403
>