PHP code example of xultech / laravel-ip-whitelist

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

    

xultech / laravel-ip-whitelist example snippets


return [
    'enabled' => true,
    'storage' => 'config', // or 'database'

    'whitelisted_ips' => [
        '127.0.0.1',
        '192.168.*.*',
        '10.0.0.0/16',
    ],

    'table' => 'ip_whitelist_entries',
    'table_prefix' => '',

    'allow_env_override' => true,
    'env_key' => 'IP_WHITELIST_OVERRIDE',

    'response' => [
        'type' => 'abort', // redirect | json | abort
        'redirect_to' => '/unauthorized',
        'json' => [
            'message' => 'Your IP is not authorized.',
            'code' => 403,
        ],
        'status_code' => 403,
        'message' => 'Access denied. Your IP is not whitelisted.',
    ],
];

'ipwhitelist' => \Xultech\LaravelIpWhitelist\Middleware\IpWhitelistMiddleware::class,

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'ipwhitelist' => \Xultech\LaravelIpWhitelist\Middleware\IpWhitelistMiddleware::class,
    ]);
})

Route::middleware('ipwhitelist')->group(function () {
    Route::get('/admin', fn () => 'Protected');
});

Route::get('/admin', fn () => 'Protected')->ipWhitelisted();

use IpWhitelist;

if (IpWhitelist::isAllowed()) {
    // access granted
}

if (ip_whitelisted()) {
    // access granted
}

   use Xultech\LaravelIpWhitelist\Models\IpWhitelistEntry;
   
    IpWhitelistEntry::create([
        'ip' => '203.0.113.1',
        'active' => true,
    ]);

    // Query active entries
     $allowed = IpWhitelistEntry::where('active', true)->pluck('ip');
    

'table' => 'ip_whitelist_entries',
'table_prefix' => '',

'table' => 'entries',
'table_prefix' => 'security_',
// Resolves to `security_entries`
bash
php artisan vendor:publish --provider="Xultech\LaravelIpWhitelist\IpWhitelistServiceProvider" --tag=ipwhitelist-config
bash
    php artisan migrate
    
bash
php artisan ipwhitelist:add 203.0.113.1 --store=database
php artisan ipwhitelist:remove 203.0.113.1 --store=database
php artisan ipwhitelist:list --store=database