PHP code example of vpndetector / laravel-vpn-detector

1. Go to this page and download the library: Download vpndetector/laravel-vpn-detector 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/ */

    

vpndetector / laravel-vpn-detector example snippets


$result = app(VpnDetectionServiceInterface::class)->checkProxyType('1.2.3.4');

$result->isBlocked();       // true for VPN | TOR | PUB | WEB
$result->isVpn();
$result->isTor();
$result->isPublicProxy();
$result->isWebProxy();
$result->isClean();         // true when proxy_type === '-'
$result->toArray();         // ['ip', 'proxy_type', 'country_code', 'country_name', 'blocked']

use VpnDetector\Facades\VpnDetector;

VpnDetector::isUsingProxy();
VpnDetector::checkProxyType('1.2.3.4');
VpnDetector::getClientIp();

return [
    // IP2Proxy download token (R_IP2PROXY_TOKEN'),

    // HTTP status returned by BlockVpnMiddleware (default: 403)
    'block_status_code' => env('VPN_DETECTOR_BLOCK_STATUS', 403),

    // Cache lookup results (strongly recommended in production)
    'cache' => [
        'enabled' => env('VPN_DETECTOR_CACHE_ENABLED', true),
        'store'   => env('VPN_DETECTOR_CACHE_STORE', null), // null = Laravel default
        'ttl'     => env('VPN_DETECTOR_CACHE_TTL', 3600),   // seconds
    ],

    // Scheduler settings for automatic database refresh
    'schedule' => [
        'frequency'           => env('VPN_DETECTOR_SCHEDULE_FREQUENCY', 'daily'),
        'daily_at'            => env('VPN_DETECTOR_SCHEDULE_AT', '02:00'),
        'timezone'            => env('VPN_DETECTOR_SCHEDULE_TIMEZONE', null),
        'environments'        => ['production', 'staging'],
        'without_overlapping' => true,
        'run_in_background'   => true,
    ],
];

use VpnDetector\Http\Middleware\BlockVpnMiddleware;

// Single route
Route::get('/checkout', CheckoutController::class)
    ->middleware(BlockVpnMiddleware::class);

// Route group
Route::middleware(BlockVpnMiddleware::class)->group(function () {
    Route::get('/dashboard', DashboardController::class);
    Route::get('/account', AccountController::class);
});

use VpnDetector\Action\CheckVpnBlocked;

class EnsureNotTor
{
    public function handle(Request $request, Closure $next)
    {
        if (app(CheckVpnBlocked::class)->handle()) {
            return redirect()->route('login')->withErrors('Session terminated for security reasons.');
        }

        return $next($request);
    }
}

use VpnDetector\Contracts\VpnDetectionServiceInterface;

class SecurityController extends Controller
{
    public function __construct(private VpnDetectionServiceInterface $vpn) {}

    public function status(): JsonResponse
    {
        $ip    = $this->vpn->getClientIp();
        $entry = $this->vpn->checkProxyType($ip);

        return response()->json([
            'ip'         => $ip,
            'proxy_type' => $entry?->proxy_type,
            'country'    => $entry?->country_name,
            'blocked'    => $this->vpn->isUsingProxy(),
        ]);
    }
}

use VpnDetector\Traits\SchedulesVpnDetector;

class Kernel extends ConsoleKernel
{
    use SchedulesVpnDetector;

    protected function schedule(Schedule $schedule): void
    {
        // Uses frequency from config/vpn-detector.php (default: daily at 02:00)
        $this->scheduleVpnDetector($schedule);

        // Or use the production preset (daily at 02:00, background, no overlap)
        $this->scheduleVpnDetectorForProduction($schedule);

        // Or pass a custom cron expression
        $this->scheduleVpnDetector($schedule, '0 3 * * 0'); // every Sunday at 03:00
    }
}
bash
# Publish the config file to config/vpn-detector.php
php artisan vendor:publish --tag=vpn-detector-config

# Publish the migrations
php artisan vendor:publish --tag=vpn-detector-migrations

# Run the migrations (creates ip2proxy_px2_ipv4 and ip2proxy_px2_ipv6 tables)
php artisan migrate
bash
php artisan vpn-detector:update-database