IP2Location PHP

Official PHP module for resolving IP addresses to geolocation and network data

Library
Composer
v9.8.1
216stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
46/100Fair
Development Activity12
Maintenance20
Community72
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
77/100Good
Architecture76
Code Quality78
Innovation68
Learning Curve84

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 WebService client for the hosted IP2Location API when you prefer not to ship a database
  • IpTools utilities 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search