PHP code example of suprun-bohdan / laravel-ip-info

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

    

suprun-bohdan / laravel-ip-info example snippets


// Helpers (v4.2+)
$country = client_country();
$country = client_country(default: 'XX');
$city = client_city(); // v4.6+, city edition or when MMDB has city data

// Fluent
if (ip_info()->isCountry('UA', 'PL')) {
    // ...
}
$city = ip_info()->city();           // v4.6+
$region = ip_info()->region();       // v4.6+
$tz = ip_info()->timezone();         // v4.6+
$coords = ip_info()->coordinates();  // v4.6+ ['lat' => ..., 'lon' => ...]

// Request macros
$country = request()->clientCountry();
$city = request()->clientCity();     // v4.6+
$ip = request()->clientIp();

// Classic facade
use SuprunBohdan\IpInfo\Laravel\Facades\IpInfo;

IpInfo::for('8.8.8.8')->countryCode();
IpInfo::forRequest(request())->countryCode();

Route::middleware(['ip.resolve', 'geo.block:RU,BY'])->group(function () {
    // ResolveClientIp + block listed countries
});

Route::middleware('geo.allow:UA,PL')->group(function () {
    // Allow-list only
});

Route::middleware('geo.share')->group(function () {
    // Shares ClientGeoData; Inertia apps get shared prop `geo`
});

Route::middleware(['ip.resolve', 'ip.log', 'ip.filter'])->group(function () {
    // Resolve + log + filter (Tor, proxy, WHOIS netname/ASN, etc.)
});

$intel = client_ip_intel(withWhois: true);
$intel->geo->countryCode();
$intel->whois?->netname;
$intel->whois?->originAsn;

php artisan ip-info:whois 8.8.8.8

// Risk score (does not block by itself)
$risk = client_ip_risk();
$risk->score;   // 0–100
$risk->level;   // low | medium | high
$risk->signals; // [{reason, points}, ...]

request()->clientIpRisk()->isHigh();
is_verified_crawler(); // reverse DNS + forward confirm

'filtering' => [
    'responses' => [
        'tor' => ['status' => 451, 'message' => 'Tor connections are not allowed.'],
        'default' => ['status' => 403, 'message' => 'Access denied.'],
    ],
    'expose_block_reason_header' => true, // X-Ip-Info-Block-Reason
],
'risk' => [
    'weights' => ['tor' => 40, 'proxy' => 25, 'hosting' => 15, ...],
    'thresholds' => ['medium' => 30, 'high' => 60],
],
'verified_crawlers' => [
    'enabled' => true,
    'skip_filtering' => true, // Googlebot etc. bypass ip.filter
],

use SuprunBohdan\IpInfo\Laravel\Events\ClientIpBlocked;

Event::listen(ClientIpBlocked::class, function (ClientIpBlocked $event) {
    // $event->reason, $event->status, $event->intel
});

use SuprunBohdan\IpInfo\Laravel\Facades\IpInfo;
use SuprunBohdan\IpInfo\Testing\InteractsWithIpInfo;

class ExampleTest extends TestCase
{
    use InteractsWithIpInfo;

    public function test_country(): void
    {
        $this->fakeIpInfo(['203.0.113.1' => 'UA']);

        $this->assertSame('UA', client_country());
        $this->assertTrue(ip_info('203.0.113.1')->isCountry('UA'));
    }
}

client_city();
ip_info('8.8.8.8')->city();
ip_info()->region();
ip_info()->timezone();
ip_info()->coordinates(); // ['lat' => float, 'lon' => float]|null
bash
php artisan ip-info:sync
php artisan ip-info:sync --json
php artisan ip-info:sync --fix
php artisan ip-info:sync --register-middleware --force
php artisan ip-info:sync --with-schedule