PHP code example of abdulhadii777 / laravel-ip-guard
1. Go to this page and download the library: Download abdulhadii777/laravel-ip-guard 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/ */
abdulhadii777 / laravel-ip-guard example snippets
return [
/*
|--------------------------------------------------------------------------
| Enable/Disable IP Guard
|--------------------------------------------------------------------------
| Set to false to disable IP restrictions entirely.
| You can also use IP_GUARD_ENABLED environment variable.
*/
'enabled' => env('IP_GUARD_ENABLED', true),
/*
|--------------------------------------------------------------------------
| IP Lists (Fallback Configuration)
|--------------------------------------------------------------------------
| These are used as fallback when database is unavailable.
| Use '*' to match all IPs. Supports exact IPs only.
|
| Priority order:
| 1. Blacklist (highest priority) - if IP matches, block access
| - '*' in blacklist blocks ALL IPs regardless of whitelist
| 2. Whitelist - if IP matches, allow access (only if not blacklisted)
| 3. null/empty - no restrictions (only if not blacklisted)
|
| Example: blacklist=['*'] + whitelist=['1.2.3.4'] = NO ACCESS (all blocked)
*/
'whitelist' => null, // Fallback whitelist (use database for dynamic management)
'blacklist' => null, // Fallback blacklist (use database for dynamic management)
/*
|--------------------------------------------------------------------------
| Client IP Resolution
|--------------------------------------------------------------------------
| Set the header to use for client IP when behind a proxy/load balancer.
| Set to null to use Laravel's default IP detection.
*/
'ip_header' => 'X-Forwarded-For', // or 'X-Real-IP', 'CF-Connecting-IP', etc.
/*
|--------------------------------------------------------------------------
| Error Response
|--------------------------------------------------------------------------
| Configure the response when access is denied.
*/
'error' => [
'status' => 403,
'message' => 'Access denied from your IP address.',
'json' => true, // Return JSON response if true, plain text if false
],
];
use Ahs\LaravelIpGuard\Facades\LaravelIpGuard;
// Add IPs to whitelist
LaravelIpGuard::addToWhitelist('192.168.1.100', 'Office IP');
LaravelIpGuard::addToWhitelist('203.0.113.10', 'Admin IP');
// Add IPs to blacklist
LaravelIpGuard::addToBlacklist('10.0.0.50', 'Blocked IP');
LaravelIpGuard::addToBlacklist('*', 'Block all IPs');
// Check IP status
$isWhitelisted = LaravelIpGuard::isWhitelisted('192.168.1.100');
$isBlacklisted = LaravelIpGuard::isBlacklisted('10.0.0.50');
// Get IP lists
$whitelistIps = LaravelIpGuard::getWhitelistIps();
$blacklistIps = LaravelIpGuard::getBlacklistIps();
// Remove IPs
LaravelIpGuard::removeFromWhitelist('192.168.1.100');
LaravelIpGuard::removeFromBlacklist('10.0.0.50');
// Bulk operations
$ips = ['192.168.1.101', '192.168.1.102', '192.168.1.103'];
LaravelIpGuard::bulkAddToWhitelist($ips, 'Office Network');
// Get statistics
$stats = LaravelIpGuard::getStats();
// Returns: ['whitelist_count' => 5, 'blacklist_count' => 2, 'total_count' => 7]
use Ahs\LaravelIpGuard\Models\IpGuard;
// Direct model usage
IpGuard::addToWhitelist('192.168.1.100', 'Office IP');
IpGuard::addToBlacklist('10.0.0.50', 'Blocked IP');
// Query IPs
$whitelistIps = IpGuard::whitelist()->active()->get();
$blacklistIps = IpGuard::blacklist()->active()->get();
// Toggle IP status
$ip = IpGuard::find(1);
$ip->toggleActive(); // Toggle between active/inactive
// Check if IP is in list
$isWhitelisted = IpGuard::isWhitelisted('192.168.1.100');
$isBlacklisted = IpGuard::isBlacklisted('10.0.0.50');
// Apply to specific routes
Route::get('/admin', function () {
return view('admin');
})->middleware('ip.guard');
// Apply to route groups
Route::middleware(['ip.guard'])->group(function () {
Route::get('/dashboard', 'DashboardController@index');
Route::get('/profile', 'ProfileController@index');
});
// Apply to controllers
Route::get('/admin', 'AdminController@index')->middleware('ip.guard');
protected $middleware = [
// ... other middleware
\Ahs\LaravelIpGuard\Middleware\IpGuard::class,
];
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('ip.guard');
}
}
'whitelist' => ['203.0.113.10', '198.51.100.20']
'blacklist' => ['*'] // Block all IPs (useful with whitelist)
'blacklist' => ['*'],
'whitelist' => ['1.2.3.4', '5.6.7.8']
// Result: NO ACCESS (all IPs blocked by blacklist)
'blacklist' => ['192.168.1.100'],
'whitelist' => ['1.2.3.4', '5.6.7.8']
// Result: Only 1.2.3.4 and 5.6.7.8 allowed, 192.168.1.100 blocked, others blocked
'blacklist' => null,
'whitelist' => null
// Result: ALL ACCESS (no restrictions)
'ip_header' => 'X-Forwarded-For', // Most common
// or
'ip_header' => 'X-Real-IP', // Nginx
// or
'ip_header' => 'CF-Connecting-IP', // Cloudflare
'error' => [
'status' => 403,
'message' => 'Custom access denied message',
'json' => false, // Use plain text instead of JSON
],