PHP code example of ipregistry / ipregistry-laravel
1. Go to this page and download the library: Download ipregistry/ipregistry-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/ */
use Ipregistry\Laravel\Facades\Ipregistry;
$info = Ipregistry::lookup('54.85.132.205');
$list = Ipregistry::lookupBatch(['8.8.8.8', '1.1.1.1']);
$origin = Ipregistry::lookupOrigin(); // your server's own IP
$agents = Ipregistry::parseUserAgents($userAgent);
Ipregistry::forRequest($request); // same as $request->ipregistry()
use Ipregistry\Laravel\Http\Middleware\EnrichWithIpregistry;
Route::middleware(EnrichWithIpregistry::using('ip', 'location', 'security'))->group(function () {
// $request->ipregistry() answers from memory in here.
});
// Equivalent alias syntax:
Route::middleware('ipregistry:ip,location,security')->group(...);
use Ipregistry\Laravel\Http\Middleware\BlockCountries;
// Block visitors from the listed countries:
Route::middleware(BlockCountries::block('KP', 'IR'))->group(...);
// Or only allow visitors from the listed countries:
Route::middleware(BlockCountries::allow('FR', 'BE'))->group(...);
// Equivalent alias syntax:
Route::middleware('ipregistry.countries:block,KP,IR')->group(...);
use Ipregistry\Laravel\Http\Middleware\BlockThreats;
// Threats, attackers, and abusers:
Route::middleware(BlockThreats::including())->group(...);
// Additionally block proxies, Tor, and VPNs:
Route::middleware(BlockThreats::including('proxy', 'tor', 'vpn'))->group(...);
// Equivalent alias syntax:
Route::middleware('ipregistry.threats:proxy,tor,vpn')->group(...);
if ($request->ipregistry()?->security->isTor) {
abort(403, 'Not available over Tor.');
}
use Ipregistry\Laravel\Facades\Ipregistry;
if (Ipregistry::isEu($request)) {
// Show the cookie consent banner.
}
// Default to showing consent UIs when the data is missing:
Ipregistry::isEu($request, assumeEu: true);
Ipregistry::isThreat($request, tor: true, vpn: true);
Ipregistry::isBot($request); // lightweight User-Agent heuristic, no API call
use Ipregistry\Laravel\Facades\Ipregistry;
public function test_eu_visitors_see_consent_banner(): void
{
Ipregistry::fake([
'*' => ['location' => ['country' => ['code' => 'FR'], 'in_eu' => true]],
]);
$this->get('/')->assertSee('cookie-consent');
}
public function test_tor_visitors_cannot_checkout(): void
{
$fake = Ipregistry::fake([
'1.2.3.4' => ['security' => ['is_tor' => true]],
]);
$this->withServerVariables(['REMOTE_ADDR' => '1.2.3.4'])
->post('/checkout')
->assertForbidden();
$fake->assertLookedUp('1.2.3.4');
}
sh
php artisan vendor:publish --tag=ipregistry-config
sh
php artisan ipregistry:lookup 8.8.8.8
php artisan ipregistry:lookup 8.8.8.8 1.1.1.1 --fields=location,security
php artisan ipregistry:lookup --hostname # your server's own IP
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.