PHP code example of skitlabs / ip-restriction-for-laravel

1. Go to this page and download the library: Download skitlabs/ip-restriction-for-laravel 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/ */

    

skitlabs / ip-restriction-for-laravel example snippets


use Illuminate\Support\Facades\Route;

// Direct IP/CIDR configuration
Route::post('/api/internal', [Controller::class, 'handle'])
    ->middleware('ip.allow:192.168.1.50,10.0.0.0/16,::1');

// Mixed named groups and direct IPs
Route::get('/api/dashboard', [Controller::class, 'index'])
    ->middleware('ip.allow:office,203.0.113.20');

// Blacklisting bad actors
Route::post('/login', [Controller::class, 'attempt'])
    ->middleware('ip.block:known_bad_ips');

// Log ALL attempts to the 'security' log channel
Route::post('/webhooks/stripe', [Controller::class, 'handle'])
    ->middleware('ip.allow:webhooks,log:all,channel:security');

// Disable logging entirely
Route::get('/health-check', [Controller::class, 'index'])
    ->middleware('ip.allow:monitoring,log:none');

Route::middleware(['ip.allow:config:api_restrictions,office'])->group(function () {
    // Middleware now uses `config/api_restrictions.php` to determine its defaults
});

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
use Skitlabs\IpRestriction\Http\Middleware\AllowIpAddress;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        // Append globally with dynamic configuration
        $middleware->append(
            AllowIpAddress::configure(
                rules: ['office', '10.0.0.0/8'],
                logLevel: 'all',
                logChannel: 'security'
            )
        );
    })->create();

use Skitlabs\IpRestriction\Http\Middleware\AllowIpAddresses;

Route::get('/admin', [Controller::class, 'index'])
    ->middleware(AllowIpAddresses::configure(
        rules: ['office', '127.0.0.1'], 
        logLevel: 'all',
    ));

return [
    // Toggle the middleware globally
    'enabled' => env('IP_RESTRICTION_ENABLED', true),

    // Environments where the middleware will quietly pass (e.g., local development)
    'ignored_environments' => ['local', 'testing'],

    // Define reusable groups of IPs
    'groups' => [
        'office'   => ['192.168.1.0/24'],
        'webhooks' => ['198.51.100.14', '2001:0db8:85a3::/64'],
        'public'   => ['0.0.0.0/0', '::/0'],
        // Passing lists as strings is supported. Extra spaces or trailing commas are trimmed 
        'dynamic'  => env('ALLOWED_IPS', '192.168.1.50,  10.0.0.0/8, '), 
    ],

    // Configure the level and channel to log to
    'logging' => [
        'level'   => env('IP_RESTRICTION_LOG_LEVEL', 'denied'),
        'channel' => env('IP_RESTRICTION_LOG_CHANNEL', 'default'),
    ],

    // Configure the returned response, for when access has been denied
    'response' => [
        'code'    => 403,
        'message' => 'Access denied (IP)',
    ],
    
    // Specify custom header if behind a non-configured TrustedProxy (e.g., 'HTTP_CF_CONNECTING_IP')
    'custom_header' => null, 
];
sh
php artisan vendor:publish --tag="ip-restriction-config"