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();