PHPackages                             middlewares/geolocation - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. middlewares/geolocation

ActiveLibrary[HTTP &amp; Networking](/categories/http)

middlewares/geolocation
=======================

Middleware to geolocate the client using the ip address

v3.2.0(9mo ago)138.5k↓25%32MITPHPPHP ^7.2 || ^8.0CI passing

Since Oct 10Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/middlewares/geolocation)[ Packagist](https://packagist.org/packages/middlewares/geolocation)[ Docs](https://github.com/middlewares/geolocation)[ RSS](/packages/middlewares-geolocation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (11)Used By (2)

middlewares/geolocation
=======================

[](#middlewaresgeolocation)

[![Latest Version on Packagist](https://camo.githubusercontent.com/20bd349268d6ff8c7a63923d554cf27f9655318769eff24faf5b82544def51c2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f67656f6c6f636174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/geolocation)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/geolocation/workflows/testing/badge.svg)](https://github.com/middlewares/geolocation/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/6e36da80236a1c87f4b9afde08533ec5d7b390eb52d880e1e0460bd650eab7cd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f67656f6c6f636174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/geolocation)[!\[SensioLabs Insight\]\[ico-sensiolabs\]](https://insight.sensiolabs.com/projects/b6c8bd3a-b3da-45ec-b2ac-9d27ae390b1b)

Middleware to geolocate the client using the ip address and [Geocoder](https://github.com/geocoder-php/Geocoder) and save the result as a request attribute.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.0
- A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)
- A [PHP-HTTP adapter](http://docs.php-http.org/en/latest/clients.html), for example [Guzzle6](https://github.com/php-http/guzzle6-adapter)
- A [Geocoder ip-based provider](https://github.com/geocoder-php/Geocoder#ip)

Installation
------------

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/geolocation](https://packagist.org/packages/middlewares/geolocation).

```
composer require middlewares/geolocation
```

Example
-------

[](#example)

```
$freeGeoIpProvider = new Geocoder\Provider\FreeGeoIp($adapter);

$dispatcher = new Dispatcher([
    new Middlewares\Geolocation($freeGeoIpProvider),

    function ($request) {
        //Get the client location
        $location = $request->getAttribute('client-location');

        $country = $location->first()->getCountry();
    }
]);

$response = $dispatcher->dispatch(new ServerRequest());
```

Options
-------

[](#options)

#### `__construct(Geocoder\Provider\Provider $provider)`

[](#__constructgeocoderproviderprovider-provider)

The geocoder provider used to geolocate the client.

It's also recommended to configure it to [caching responses.](https://github.com/geocoder-php/Geocoder/blob/master/docs/cookbook/cache.md)

#### `ipAttribute(string $ipAttribute)`

[](#ipattributestring-ipattribute)

By default uses the `REMOTE_ADDR` server parameter to get the client ip. This option allows to use a request attribute. Useful to combine with a ip detection middleware, for example [client-ip](https://github.com/middlewares/client-ip).

#### `attribute(string $attribute)`

[](#attributestring-attribute)

The attribute name used to store the client addresses in the server request. By default is `client-location`.

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance57

Moderate activity, may be stable

Popularity30

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 86.3% 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 ~357 days

Recently: every ~686 days

Total

10

Last Release

286d ago

Major Versions

v0.5.0 → v1.0.02018-01-27

v1.0.0 → v2.0.02018-08-04

v2.0.0 → v3.0.02020-12-04

PHP version history (3 changes)v0.1.0PHP ^5.6 || ^7.0

v0.5.0PHP ^7.0

v3.0.0PHP ^7.2 || ^8.0

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (44 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (5 commits)")[![steffans](https://avatars.githubusercontent.com/u/321013?v=4)](https://github.com/steffans "steffans (2 commits)")

---

Tags

geolocationhttpmiddlewarepsr-15httppsr-7middlewareservergeolocationpsr-15IPgeocode

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/middlewares-geolocation/health.svg)

```
[![Health](https://phpackages.com/badges/middlewares-geolocation/health.svg)](https://phpackages.com/packages/middlewares-geolocation)
```

###  Alternatives

[middlewares/client-ip

Middleware to detect the client ip and save it as a request attribute

16629.7k9](/packages/middlewares-client-ip)[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[middlewares/firewall

Middleware to provide IP filtering

12318.3k2](/packages/middlewares-firewall)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[middlewares/negotiation

Middleware to implement content negotiation

47442.1k11](/packages/middlewares-negotiation)[middlewares/payload

Middleware to parse the body of the request with support for json, csv and url-encode

32466.8k17](/packages/middlewares-payload)

PHPackages © 2026

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