PHPackages                             netglue/geoip2-helpers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. netglue/geoip2-helpers

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

netglue/geoip2-helpers
======================

1.0.3(7y ago)034MITPHPPHP &gt;=7.1CI failing

Since Aug 21Pushed 7y ago1 watchersCompare

[ Source](https://github.com/netglue/GeoIP-Helper)[ Packagist](https://packagist.org/packages/netglue/geoip2-helpers)[ RSS](/packages/netglue-geoip2-helpers/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (15)Versions (5)Used By (0)

Geo Location Helper/Middleware for Zend Framework/Expressive Projects
=====================================================================

[](#geo-location-helpermiddleware-for-zend-frameworkexpressive-projects)

[![Latest Stable Version](https://camo.githubusercontent.com/b7318535177cae8fcb653051b5bc59d92023e7d256567258c526c84f29b54e86/68747470733a2f2f706f7365722e707567782e6f72672f6e6574676c75652f67656f6970322d68656c706572732f76657273696f6e)](https://packagist.org/packages/netglue/geoip2-helpers)[![Coverage Status](https://camo.githubusercontent.com/04352dfd346f2a7c31975401533afb6f66ea8852904dc855744651fdd8b87154/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6e6574676c75652f47656f49502d48656c7065722f62616467652e737667)](https://coveralls.io/github/netglue/GeoIP-Helper)[![Build Status](https://camo.githubusercontent.com/1fd39f3c18049010b97d80c3a8bb0cd828f51ff5f995a2aa5125dd70f0dde13f/68747470733a2f2f7472617669732d63692e6f72672f6e6574676c75652f47656f49502d48656c7065722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/netglue/GeoIP-Helper)[![Maintainability](https://camo.githubusercontent.com/9ecda0d86676a6b304ace2f2fab30cc5384b47513cb7c85434bb8130e45418e5/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35313937346463346366643633643632663566332f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/netglue/GeoIP-Helper/maintainability)[![Test Coverage](https://camo.githubusercontent.com/8c769f923fee7f95f5c33f8d41cf20739b266aa92c76da962557540016d9092c/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35313937346463346366643633643632663566332f746573745f636f766572616765)](https://codeclimate.com/github/netglue/GeoIP-Helper/test_coverage)

About
-----

[](#about)

This lib is useful if you are only interested in using MaxMind’s GeoLite2 databases to figure out the country of origin and maybe a bit more detail such as city (slower) based on the IP address of the visitor inside a Zend Expressive or PSR-15 app, or a Zend Framework 3 based app. Factories and default configuration are setup out of the box for both Expressive and ZF3.

Retrieving location data for an IP address relies on the ability to figure out the remote address of the client, so this lib has a dependency on [netglue/realip-helpers](https://github.com/netglue/RealIP-Helpers). This is only used by the middleware.

Install
-------

[](#install)

Install with composer using `"netglue/geoip2-helpers"`

Zend’s component installer

Get a copy of the database
--------------------------

[](#get-a-copy-of-the-database)

This package comes with a utility for downloading the free, GeoLite2 databases. If you are using a paid version, you’ll probably be downloading the databases into a central location, so MaxMind’s `geoipupdate` will likely be a better tool for you: .

### Using the shipped downloader tool

[](#using-the-shipped-downloader-tool)

After installing with composer, the command will be made available in `vendor/bin/geoip`. You can get help for the command with `geoip help geoip:download`.

With no arguments, the command will download both the city and country databases into the `./data` directory of this lib. To download to a different directory, you should provide a directory argument, i.e `geoip geoip:download ~/geoip-data/here`.

Files will be named whatever they are on the remote server, something like `GeoLite2-City.mmdb`.

If you are only interested in Country level information, you can turn off downloading the city database with the `--country`switch and vice-versa with `--city`. By default, both databases will be retrieved.

The `--no-clobber` switch will only perform a download if the files do not already exist so it can be used during deployment for example to avoid repeatedly downloading stuff you don't need.

ZF3 Controller Plugin
---------------------

[](#zf3-controller-plugin)

In a ZF3 app, the default config exposes a plugin with the name `geoIp`. Using this plugin is pretty straight forward:

```
$ip = $this->clientIp(); // If you are using the netglue/realip-helpers dependency
if ($ip) {
    $countryCode = $this->geoIp()->countryCode($ip);
    $countryName = $this->geoIp()->countryName($ip);
    $timezone    = $this->geoIp()->timezone($ip); // If you are using the City Database
    $dataArray   = $this->geoIp()->get($ip);
}
```

PSR-15 Middleware
-----------------

[](#psr-15-middleware)

Add `\NetglueGeoIP\Middleware\Geolocation::class` to your pipeline, after a middleware that will inject a request attribute of `ip_address` *(By default, can be configured in construct)* containing the client IP.

Subsequent middleware will have access to the attributes:

- `Geolocation::DATA` - An array containing everything that was found in the database (If anything)
- `Geolocation::COUNTRY_CODE` - 2 letter ISO country code
- `Geolocation::COUNTRY_NAME` - Country name according to configured locales
- `Geolocation::TIMEZONE` - Timezone, if available - depends on using the city based database.

Alternative stuff to look at
----------------------------

[](#alternative-stuff-to-look-at)

This lib might be too narrow or specific so you might find that [Geocoder PHP](http://geocoder-php.org) is a more mature, better fit for your needs. There's PSR-15 middleware out there that consumes this lib: [middlewares/geolocation](https://github.com/middlewares/geolocation)

Test
----

[](#test)

`cd` to wherever the module is installed, issue a `composer install` followed by a `composer test`.

Contributions
-------------

[](#contributions)

PR's are welcomed. Please write tests for new features.

Support
-------

[](#support)

You're welcome to file issues, but please understand that finding the time to answer support requests is very limited so there might be a long wait for an answer.

About
-----

[](#about-1)

[Netglue makes websites and apps in Devon, England](https://netglue.uk). We hope this is useful to you and we’d appreciate feedback either way :)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

2823d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a6c21b0de8cc9938916a186f5f2ed0bee90a4e23e0b29cf7b9c18bfe99067f9b?d=identicon)[netglue](/maintainers/netglue)

---

Top Contributors

[![gsteel](https://avatars.githubusercontent.com/u/2803720?v=4)](https://github.com/gsteel "gsteel (18 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/netglue-geoip2-helpers/health.svg)

```
[![Health](https://phpackages.com/badges/netglue-geoip2-helpers/health.svg)](https://phpackages.com/packages/netglue-geoip2-helpers)
```

###  Alternatives

[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

73142.3k25](/packages/jaxon-php-jaxon-core)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)[fisharebest/webtrees

webtrees online genealogy

73710.5k13](/packages/fisharebest-webtrees)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
