The official Laravel middleware for the InternetData API.
It looks up the visitor behind each request, where they are, which network they come from and whether it's a VPN, a proxy, Tor or a hosting server, and hands the answer to your code. Blocking is opt-in.
composer require internetdata/laravelRequires PHP 8.2 or newer and Laravel 10, 11, 12 or 13.
A lookup needs no key to try, and answers the basics: the country and the network behind the address, Tor, relay and anycast space. The company behind the address and its abuse contact are not among them: they come with a free key's full answers or a paid plan, and read null without a key. Every lookup your app makes comes from your server's one address, though, so a busy site will see bursts refused, and its lookups without a key used up for the day. A free key answers every field on its organization's first lookups each UTC day, then a lookup is quota_exceeded with the basics, and a paid plan answers every field, VPN, the proxy flags, hosting, CDN, bots, carrier and satellite space, the service flags, the network's type and the city among them; create a key in the console.
The service provider is auto-discovered. Publish the config and register the middleware:
php artisan vendor:publish --tag=internetdata-config// bootstrap/app.php (Laravel 11+)
->withMiddleware(function (Middleware $middleware) {
$middleware->append(\InternetData\Laravel\InternetDataMiddleware::class);
})Set INTERNETDATA_API_KEY in your .env.
use InternetData\Laravel\InternetDataMiddleware;
Route::get("/", function (Request $request) {
$result = InternetDataMiddleware::lookup($request)?->result;
return "Hello, visitor from " . ($result?->location->countryName ?? "somewhere");
});By default nothing is blocked. Every request gets an answer and your own code decides what it means: a currency to show, a sign-up to review, a page to keep from some countries.
Set a block_condition and a matching request is answered with 403 and never reaches your code.
// config/internetdata.php
'block_condition' => ['location' => ['country' => ['CU', 'IR', 'KP']]],A condition is written in the shape of a Result, and only the members you name are considered, so it can refuse on anything in the answer. The country works on every plan, and so does the first of these:
['isAnycast' => true] // anycast space
['asn' => ['type' => 'hosting']] // the network's type
['isBot' => true] // crawlers, monitors and scanners
[['isSatellite' => true], ['isCarrier' => true]] // satellite and mobile carrier space
[['isHosting' => true], ['isCdn' => true]] // traffic from servers, not people
[['isVpn' => true], ['isTor' => true], ['isResproxy' => true]] // anonymity: a list is OR
['isVpn' => true, 'vpn' => ['provider' => 'nordvpn']] // one provider
['resproxy' => ['hits' => ['gte' => 5]]] // a numeric thresholdValues are matched by equality, strings without regard to case. A LIST means any-of. A map of gte/gt/lte/lt compares numbers, and every bound you give must hold, so two of them are a range. A member the answer holds as a list, such as web's rows or pubproxy.protocols, matches when any one element does, and a nested condition must hold within that one element. Members set to false or null are ignored, so a condition states the signals you act on; one that constrains nothing would match every request, and is refused when the middleware is built rather than silently blocking all your traffic.
Set allow_bots and the middleware sends each request's own User-Agent with its lookup when it names a crawler, so the answer says whose bot it names and whether this address bears that out. A request whose verdict is verified, a crawler its operator vouches for here, is never refused, whatever block_condition matches; its answer is still attached:
'allow_bots' => true,
'block_condition' => ['isHosting' => true],With it on, a condition can also refuse a forged crawler: ['userAgent' => ['verdict' => 'spoofed']]. Any other agent, or none, is not sent, since its verdict could only be not_bot. One address is looked up with at most four crawler agents an hour, remembered in the app's cache store (cache_store) so the bound holds under PHP-FPM too (with it set to false, only within one process): past those its lookup sends none, and a request passes as a crawler only where the answer's bot.verified is true. Only an answer with every field carries the verdict, and bot is a paid member too, so a keyless app gets no bot pass and nothing is let through by it; the middleware warns once when an agent was sent and the answer had no verdict. Off by default, since on, answers are cached per address and user agent rather than per address.
This is the setting that decides whether any of the above works, and it is the one thing only you can get right.
By default the middleware uses $request->ip(), which honours Laravel's own TrustProxies middleware. That is the right fix behind a load balancer: set TrustProxies to the proxies actually in front of you and ip() walks the chain for you. Without it, and behind one, every visitor looks like the load balancer, and that is the address that gets looked up: every visitor gets its country and network, and if it is a hosting address, a hosting rule blocks everyone. A load balancer in your own network usually has a private address, which the middleware answers locally without a lookup, as an address with no country, and logs a warning.
For an edge that writes the address into its own header, name the header:
// config/internetdata.php
'ip_selector' => \InternetData\Laravel\Selectors::header('CF-Connecting-IP'),\InternetData\Laravel\Selectors::xff() reads X-Forwarded-For directly. Be aware that the left-most entry is whatever the caller sent, because proxies append to that header, so it is only trustworthy when an edge you control overwrites it. If you know how many proxies sit in front, count from the right instead: Selectors::xff(1) is the address your nearest proxy saw.
Anything else, pass your own callable. It receives the request and returns an address.
If the address resolves to a private one, the middleware says so once each time the application boots, which under PHP-FPM is every request. That is expected locally and is the signal to fix your configuration anywhere else.
The request is let through, and the reason is on the answer's error. Our outage should not become yours, so a network failure, a rate limit or a rejected key all fail open. Set fail_closed to block instead. Private addresses are answered locally and never fail, so this will not lock you out in development.
A key past its organization's allowance, a free key's full lookups for the UTC day or a paid key's quota and overage cap, gets the basics with cap_reached true, and the SDK raises that as quota_exceeded, so for the middleware it is a failed lookup too: the request is let through with nothing attached, and the lookup's error is an InternetData\CapReachedException whose result still has the basics. The error is logged once a minute, naming quota_exceeded, to PHP's error log, or to a callable under on_error in config/internetdata.php, so a site that fails open on it still hears of it. fail_closed refuses the request.
Answers are cached for an hour in your app's default cache store, keyed by a hash of your API key and the visitor's address, so a returning visitor costs nothing even under PHP-FPM, where the application is built afresh for every request. Private addresses never leave the process. A lookup is one request to our API, bounded at 2.5 seconds by default and not retried: on a request path, failing open quickly beats holding a visitor while we try again.
Name another store from config/cache.php with cache_store, or set it to false to keep answers in the client alone, which under PHP-FPM lasts one request.
Beyond a few million distinct visitors a day, stop calling the API per request: look addresses up locally in our databases instead.
The basics, which a lookup without a key answers and so does a free key's past its organization's full answers for the day, leave out company and abuse, isVpn, isHosting, isCdn, the four proxy flags, isBot, isCarrier, isSatellite and the service flags, the detail behind them, and the paid members inside location and asn, such as location->city and asn->type. A property your plan does not include is null, which means "not in your plan" rather than "checked, and no". That holds for the eight flags isBot, isCarrier, isSatellite, isSsh, isSmtp, isWeb, isDnsResolver and isBittorrent, for asn->type, and for company and abuse too: each reads null when the answer leaves it out, never false, '' or an empty object, and a condition naming one is warned about.
$lookup->result->isVpn ?? false // when you only want the flag
$lookup->result->company?->name // null with the basics, never ''A block_condition naming a member your plan does not serve, location.city or asn.type with the basics included, can never match, so the middleware warns, once each time the application boots, instead of failing silently. A term inside company or abuse is reported by that object's name, company. Set on_missing_field to throw to make it an error.
There are official InternetData client libraries available for many languages including PHP, Python, Go, Java, and Ruby. See our GitHub at https://github.com/internetdata for more.
Geolocation, anonymity, ownership, network, service and domain data for any IP address, AS or domain, by API or as databases you download.
This project is licensed under the MIT License.