Repository navigation
Client Location
Geolocator::lookup($ip) answers "where is this address?" and always takes the address as an
argument. A client locator answers "where is the caller?" — it takes no address. Instead it
reads the current request, either by resolving the request's address and looking it up, or by reading
headers an upstream proxy has already filled in.
use SameOldNick\Geolocator\Contracts\ClientLocator as ClientLocatorContract;
use SameOldNick\Geolocator\Facades\ClientLocator;
ClientLocator::lookup(); // facade
app(ClientLocatorContract::class)->lookup(); // contract
app('client-locator')->lookup(); // container alias
\ClientLocator::lookup(); // global alias, no import neededAll four resolve the same SameOldNick\Geolocator\ClientLocatorManager. Unlike the geolocator manager,
it is a scoped binding: one instance per request, discarded afterwards, so a queue worker or an
Octane process never serves the previous client's location.
ClientLocator::lookup(); // country, city and ASN
ClientLocator::lookupCountry(); // country only
ClientLocator::lookupCity(); // city only
ClientLocator::lookupAsn(); // ASN onlyEach returns the same LocationResult described in Usage, with the editions you did not ask
for left as null.
geolocator.client (or GEOLOCATOR_CLIENT) picks the implementation:
| Value | Class | Where the answer comes from |
|---|---|---|
ip |
ClientLocators\IpClientLocator |
$request->ip() handed to a geolocator driver, which reads the .mmdb files |
headers |
ClientLocators\Headers\HeadersClientLocator |
Headers injected by an upstream proxy, with no database access |
GEOLOCATOR_CLIENT=headersThe default is ip, which is what most applications want. Reach for headers when something in front
of the application has already located the caller — middleware such as the
IP2Location Caddy Middleware, a CDN or another
reverse proxy — and passes the answer along as request headers.
GEOLOCATOR_CLIENT=ip
GEOLOCATOR_CLIENT_IP_DRIVER=iplocationdb- It resolves the client address with
$request->ip(), which already honours Laravel'sTrustProxiesmiddleware. Never parseX-Forwarded-Forby hand: any client can send it. - It hands that address to the geolocator driver named by
geolocator.clients.ip.driver. That key is independent ofgeolocator.driver, so client lookups can use a different driver fromGeolocator::lookup(); set it tonull(or leave it empty) to inheritgeolocator.driverinstead. - When the request carries no address at all — a console command, a queued job, a malformed test
request — it throws
RuntimeException: Unable to determine client IP address.rather than looking up a made-up address. Check for a request, or callGeolocator::lookup($ip)with an address you have. - Private and reserved addresses are passed to the driver unfiltered, so they come back as an empty
result exactly as they do for
Geolocator::lookup().
GEOLOCATOR_CLIENT=headers
GEOLOCATOR_REQUEST_HEADERS_PREFIX=X_IP2LOCATION_Use it when the data is already on the request. Nothing is read from disk, so the
IPLOCATIONDB_*_PATH files do not have to exist for these lookups. The default X-IP2Location-* header
names are the ones the IP2Location Caddy Middleware emits, so that setup needs no mapping changes.
A header name is built as prefix + the configured mapping, then normalised to lowercase with _
replaced by -. With the package defaults, X_IP2LOCATION_ + COUNTRY_CODE therefore matches the
header X-IP2Location-Country-Code, and the request below produces a full result:
// Sent by the proxy:
// X-IP2Location-Country-Code: US
// X-IP2Location-Country-Name: United States
// X-IP2Location-City: Ashburn
// X-IP2Location-Region: Virginia
// X-IP2Location-Latitude: 39.0438
// X-IP2Location-Longitude: -77.4874
// X-IP2Location-Time-Zone: America/New_York
// X-IP2Location-ASN: 15169
// X-IP2Location-AS: Google LLC
$location = $request->geolocate();
$location->country?->countryCode; // 'US'
$location->city?->city; // 'Ashburn'
$location->asn?->organization; // 'Google LLC'| Config key | Environment variable | Default | Header becomes |
|---|---|---|---|
clients.headers.mappings.country.country_code |
GEOLOCATOR_REQUEST_HEADER_COUNTRY_CODE |
COUNTRY_CODE |
X-IP2Location-Country-Code |
clients.headers.mappings.country.country_name |
GEOLOCATOR_REQUEST_HEADER_COUNTRY_NAME |
COUNTRY_NAME |
X-IP2Location-Country-Name |
clients.headers.mappings.city.city |
GEOLOCATOR_REQUEST_HEADER_CITY |
CITY |
X-IP2Location-City |
clients.headers.mappings.city.state1 |
GEOLOCATOR_REQUEST_HEADER_STATE1 |
REGION |
X-IP2Location-Region |
clients.headers.mappings.city.state2 |
GEOLOCATOR_REQUEST_HEADER_STATE2 |
STATE2 |
X-IP2Location-State2 |
clients.headers.mappings.city.latitude |
GEOLOCATOR_REQUEST_HEADER_LATITUDE |
LATITUDE |
X-IP2Location-Latitude |
clients.headers.mappings.city.longitude |
GEOLOCATOR_REQUEST_HEADER_LONGITUDE |
LONGITUDE |
X-IP2Location-Longitude |
clients.headers.mappings.city.timezone |
GEOLOCATOR_REQUEST_HEADER_TIMEZONE |
TIME_ZONE |
X-IP2Location-Time-Zone |
clients.headers.mappings.asn.asn |
GEOLOCATOR_REQUEST_HEADER_ASN |
ASN |
X-IP2Location-ASN |
clients.headers.mappings.asn.organization |
GEOLOCATOR_REQUEST_HEADER_ORGANIZATION |
AS |
X-IP2Location-AS |
How the values are read:
- Only the first value of a repeated header is used.
- An edition is built only from complete data: a country needs a code the package recognises, a city
needs a country code, and an ASN needs a numeric
asnand a non-empty organization. Anything missing, empty or unrecognised leaves that editionnull— no exception. -
country_nameoverrides the name the package would derive from the code, which is how a proxy can localise it. -
ipAddressreports$request->ip(). When the request carries no address, it falls back togeolocator.clients.headers.fallback_ip(0.0.0.0), so the header data is still returned. - Set
GEOLOCATOR_REQUEST_HEADERS_PREFIX=(empty) and put the full header name in the mapping when the proxy does not use a shared prefix.
The headers path trusts the proxy. Make sure the headers cannot reach the application from the public internet — strip or overwrite them at the edge — or a client can choose its own location.
The geolocate macro on Illuminate\Http\Request is a thin wrapper over the configured client
locator, which makes it the shortest way to locate the caller:
$request->geolocate(); // lookup(): country, city and ASN
$request->geolocate('country'); // lookupCountry()
$request->geolocate(edition: 'city'); // lookupCity()
$request->geolocate(edition: 'asn'); // lookupAsn()
$request->geolocate(driver: 'headers'); // a client locator other than the configured one- The first argument is the edition:
all(the default),country,cityorasn. Anything else, including a wrong case, performs the full lookup. -
driver:accepts any name registered on the client locator manager —ip,headers, or one you registered withClientLocator::extend(). An unknown name throwsInvalidArgumentException: Driver [x] not supported. - There is no default-address argument. To look up a specific address, call
Geolocator::lookup($ip)instead — see Upgrading if you are coming from a version where the first argument was one.
ClientLocator::fake() swaps the facade root for an in-memory fake, so the lookup never reads a
request or a database:
use SameOldNick\Geolocator\DTOs\CountryResult;
use SameOldNick\Geolocator\DTOs\LocationResult;
use SameOldNick\Geolocator\Facades\ClientLocator;
ClientLocator::fake(new LocationResult(
ipAddress: '8.8.8.8',
country: CountryResult::create('US'),
city: null,
asn: null,
));
ClientLocator::lookup()->country?->countryCode; // 'US'The pinned result is returned as-is by lookup(); the edition-specific methods return it with the
other editions cleared. With no argument, fake() answers every lookup with an empty result for
0.0.0.0. See Testing for restoring the real manager and for faking the geolocator instead.
Implement SameOldNick\Geolocator\Contracts\ClientLocator, register it with ClientLocator::extend(),
and select it with GEOLOCATOR_CLIENT. See Custom drivers.