- Notifications
You must be signed in to change notification settings - Fork80
PHP Reader for the MaxMind DB Database Format
License
maxmind/MaxMind-DB-Reader-php
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is the PHP API for reading MaxMind DB files. MaxMind DB is a binary fileformat that stores data indexed by IP address subnets (IPv4 or IPv6).
We recommend installing this package withComposer.
To download Composer, run in the root directory of your project:
curl -sS https://getcomposer.org/installer| phpYou should now have the filecomposer.phar in your project directory.
Run in your project root:
php composer.phar require maxmind-db/reader:^1.12.1You should now have the filescomposer.json andcomposer.lock as well asthe directoryvendor in your project directory. If you use a version controlsystem,composer.json should be added to it.
After installing the dependencies, you need to require the Composer autoloaderfrom your code:
require'vendor/autoload.php';
If you don't want to use Composer for some reason, a customautoload.php is provided for you in the project root. To use thelibrary, simply include that file,
require('/path/to/MaxMind-DB-Reader-php/autoload.php');
and then instantiate the reader class normally:
useMaxMind\Db\Reader;$reader =newReader('example.mmdb');
RPMs are available in theofficial Fedora repository.
To install on Fedora, run:
dnf install php-maxminddb
To install on CentOS or RHEL 7, firstenable the EPEL repositoryand then run:
yum install php-maxminddb
Please note that these packages arenot maintained by MaxMind.
<?phprequire_once'vendor/autoload.php';useMaxMind\Db\Reader;$ipAddress ='24.24.24.24';$databaseFile ='GeoIP2-City.mmdb';$reader =newReader($databaseFile);// get returns just the record for the IP addressprint_r($reader->get($ipAddress));// getWithPrefixLen returns an array containing the record and the// associated prefix length for that record.print_r($reader->getWithPrefixLen($ipAddress));$reader->close();
MaxMind provides an optional C extension that is a drop-in replacement forMaxMind\Db\Reader. In order to use this extension, you must install theReader API as described above and install the extension as described below. Ifyou are using an autoloader, no changes to your code should be necessary.
First installlibmaxminddb asdescribed in itsREADME.mdfile.After successfully installing libmaxmindb, you may install the extensionfrompecl:
pecl install maxminddbAlternatively, you may install it from the source. To do so, run the followingcommands from the top-level directory of this distribution:
cd extphpize./configuremakemake testsudo make installYou then must load your extension. The recommended method is to add thefollowing to yourphp.ini file:
extension=maxminddb.soNote: You may need to install the PHP development package on your OS such asphp5-dev for Debian-based systems or php-devel for RedHat/Fedora-based ones.
The MaxMind DB format includes 128-bit unsigned integer as a type. Althoughno MaxMind-distributed database currently makes use of this type, both thepure PHP reader and the C extension support this type. The pure PHP readerrequires gmp or bcmath to read databases with 128-bit unsigned integers.
The integer is currently returned as a hexadecimal string (prefixed with "0x")by the C extension and a decimal string (no prefix) by the pure PHP reader.Any change to make the reader implementations always return either ahexadecimal or decimal representation of the integer will NOT be considered abreaking change.
Please report all issues with this code using theGitHub issue tracker.
If you are having an issue with a MaxMind service that is not specific to theclient API, please seeour support page.
This library requires PHP 7.2 or greater.
The GMP or BCMath extension may be required to read some databasesusing the pure PHP API.
Patches and pull requests are encouraged. All code should follow the PSR-1 andPSR-2 style guidelines. Please include unit tests whenever possible.
The MaxMind DB Reader PHP API usesSemantic Versioning.
This software is Copyright (c) 2014-2025 by MaxMind, Inc.
This is free software, licensed under the Apache License, Version 2.0.
About
PHP Reader for the MaxMind DB Database Format
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.