PHP code example of digitalcorehub / laravel-ip-restriction

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

    

digitalcorehub / laravel-ip-restriction example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | Allowed IP Addresses
    |--------------------------------------------------------------------------
    |
    | This array contains the IP addresses that are allowed to access your
    | application when using the IP restriction middleware.
    |
    */
    'allowed_ips' => [
        '127.0.0.1',        // Localhost
        '::1',              // IPv6 localhost
        '192.168.1.0/24',   // Local network range
        '10.0.0.0/8',       // Private network range
    ],

    /*
    |--------------------------------------------------------------------------
    | Cache Configuration
    |--------------------------------------------------------------------------
    |
    | Configure how long the allowed IPs list should be cached.
    | Set to null to disable caching.
    |
    */
    'cache_ttl' => 3600, // 1 hour in seconds

    /*
    |--------------------------------------------------------------------------
    | Logging Configuration
    |--------------------------------------------------------------------------
    |
    | Configure whether to log unauthorized access attempts.
    |
    */
    'log_attempts' => true,

    /*
    |--------------------------------------------------------------------------
    | Custom Error Message
    |--------------------------------------------------------------------------
    |
    | Customize the error message shown when access is denied.
    |
    */
    'error_message' => 'Access denied. Your IP address is not authorized.',
];

// Bireysel route'ları koru / Protect individual routes
Route::get('/admin', function () {
    return view('admin.dashboard');
})->middleware('restrict_to_ip');

// Route gruplarını koru / Protect route groups
Route::middleware(['restrict_to_ip'])->group(function () {
    Route::get('/admin/dashboard', [AdminController::class, 'dashboard']);
    Route::get('/admin/users', [AdminController::class, 'users']);
    Route::get('/admin/settings', [AdminController::class, 'settings']);
});

// Controller'ları koru / Protect controllers
class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('restrict_to_ip');
    }
}

use DigitalCoreHub\LaravelIpRestriction\Facades\LaravelIpRestriction;

// IP'nin izin verilip verilmediğini kontrol et / Check if an IP is allowed
if (LaravelIpRestriction::isIpAllowed('192.168.1.100')) {
    // IP'ye izin verildi / IP is allowed
}

// İstemci IP'sini al / Get client IP
$clientIp = LaravelIpRestriction::getClientIp();

// İzin verilen listeye IP ekle (sadece çalışma zamanında) / Add IP to allowed list (runtime only)
LaravelIpRestriction::addAllowedIp('203.0.113.1');

// İzin verilen listeden IP çıkar (sadece çalışma zamanında) / Remove IP from allowed list (runtime only)
LaravelIpRestriction::removeAllowedIp('203.0.113.1');

// Cache'i temizle / Clear cache
LaravelIpRestriction::clearCache();

'allowed_ips' => [
    '127.0.0.1',           // Tek IPv4 adresi / Single IPv4 address
    '::1',                 // Tek IPv6 adresi / Single IPv6 address
    '192.168.1.0/24',      // IPv4 CIDR aralığı / IPv4 CIDR range
    '2001:db8::/32',       // IPv6 CIDR aralığı / IPv6 CIDR range
    '10.0.0.0/8',          // Büyük IPv4 aralığı / Large IPv4 range
    '172.16.0.0/12',       // Özel IPv4 aralığı / Private IPv4 range
],

// config/ip-restriction.php dosyasında / In config/ip-restriction.php
'error_message' => 'Üzgünüz, konumunuzdan erişime izin verilmiyor. / Sorry, access from your location is not permitted.',

// config/ip-restriction.php dosyasında / In config/ip-restriction.php
'log_attempts' => false,

// config/ip-restriction.php dosyasında / In config/ip-restriction.php
'cache_ttl' => null,
bash
php artisan vendor:publish --tag=ip-restriction-config