IP2Location PHP
Official PHP module for resolving IP addresses to geolocation and network data
Repository Health
Technical Analysis
IP2Location PHP is the official library from IP2Location.com for translating any IPv4 or IPv6 address into rich location and network information. From a single lookup you can retrieve country, region, city, latitude and longitude, ZIP code, time zone, ISP, domain, connection speed, usage type, ASN, mobile carrier codes, and more.
It works primarily against IP2Location’s file-based BIN databases for fast, offline lookups, and also includes a web-service client for querying the hosted API. A free LITE database and commercial databases with higher accuracy are available, making it suitable for everything from log analysis to fraud detection and geotargeting.
What You Get
- Offline IP-to-location lookups against IP2Location BIN database files
- Access to dozens of fields: country, region, city, coordinates, ZIP, timezone, ISP, ASN, usage type, and more
- A
WebServiceclient for the hosted IP2Location API when you prefer not to ship a database IpToolsutilities for IPv4/IPv6 parsing, conversion, and range checks- Support for both IPv4 and IPv6 addresses
- Configurable caching modes (file, shared memory, memory cache) for performance
Common Use Cases
- Determining visitor country and city from web server logs
- Geotargeting content, language, or currency by IP
- Fraud and abuse detection using ISP, usage type, and proxy signals
- Routing users to the geographically closest server or mirror
Under The Hood
Architecture - The library is a small set of PSR-4 classes under the IP2Location\ namespace. Database opens a BIN file, reads its header to learn the field layout for the licensed database type, and seeks by IP (converted to an integer via bcmath) to decode only the requested columns. WebService wraps the hosted HTTP API, Country/Region add CSV-backed enrichment, and IpTools handles IP math (validation, conversion, CIDR/range expansion).
Tech Stack - Pure PHP requiring 7.2+ with the bcmath, curl, and json extensions. It has no runtime Composer dependencies; PHPUnit (12.5+) is the only dev dependency. Data comes from external IP2Location BIN files or the web API rather than bundled code.
Code Quality - Each core class has a matching PHPUnit test (DatabaseTest, WebServiceTest, IpToolsTest, CountryTest, RegionTest) with a bootstrap, and the repo ships examples/ and docs/. The code is straightforward procedural-style PHP; binary parsing logic in Database is the most intricate area.
API Design - Usage is minimal: instantiate Database with a path and caching mode, then call lookup($ip, Database::ALL). Field access is via well-named keys, and the extensive README plus examples make the offline path easy to adopt; the main external requirement is obtaining and updating the BIN database file.