PHPackages                             danio1024/ip-user-location - 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. danio1024/ip-user-location

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

danio1024/ip-user-location
==========================

A basic PHP wrapper for integrating IP based user location detection into your application.

0483PHP

Since Dec 2Pushed 12y ago2 watchersCompare

[ Source](https://github.com/danio1024/ip-user-location)[ Packagist](https://packagist.org/packages/danio1024/ip-user-location)[ RSS](/packages/danio1024-ip-user-location/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

IP User Location
================

[](#ip-user-location)

A basic PHP wrapper for adding IP based user location detection into your application using the [IP Info DB API](http://ipinfodb.com/ip_location_api.php).

### Methods

[](#methods)

#### Get city based information

[](#get-city-based-information)

You can use `->getCity($ip)` to retrieve city level information about an IP address.

Example json response:

```
{
  "statusCode" : "OK",
  "statusMessage" : "",
  "ipAddress" : "81.149.15.65",
  "countryCode" : "GB",
  "countryName" : "UNITED KINGDOM",
  "regionName" : "ENGLAND",
  "cityName" : "SALISBURY",
  "zipCode" : "SP1 1TP",
  "latitude" : "51.0693",
  "longitude" : "-1.79569",
  "timeZone" : "+01:00"
}

```

#### Get country based information

[](#get-country-based-information)

You can use `->getCountry($ip)` to retrieve country level information about an IP address. Obviously this is faster than retrieving city level information.

Example json response:

```
{
  "statusCode" : "OK",
  "statusMessage" : "",
  "ipAddress" : "81.149.15.65",
  "countryCode" : "GB",
  "countryName" : "UNITED KINGDOM"
}

```

#### Validate an IP address

[](#validate-an-ip-address)

You can use `->validIP($ip)` to see if the specified IP Address is valid.

Under the hood this uses `filter_var($ip, FILTER_VALIDATE_IP)`, I assume this is pretty solid. If you have any suggestions for a better way of doing this send a pull request or [drop me an email](mailto:tom@beingtomgreen.com).

#### Get the user's IP address

[](#get-the-users-ip-address)

You can use `->getIpAddress()` to retrieve the users IP address.

**You shouldn't be trusting a user based this data, HTTP headers can be faked, trivially.**

### API keys &amp; query limits

[](#api-keys--query-limits)

You can get a (free) API key [here](http://ipinfodb.com/register.php), obviously this should be kept private.

While there are no strict query limits if you send more than 2 requests per second they will be queued. You will still always get a response, but it will be slowed to around 1 /second.

I would strongly suggest using some form of cache. Cookies (mmm cookies) are probably the easiest way to deal with this:

```
// Create a new instance
$ipInfo = new ipInfo (APIKEY, 'json');

// Grab the user location info
$location = json_decode($ipInfo->getCountry($userIP));

// Create a cookie holding the country code for 1 hour (3600 seconds)
setcookie('location', $location['countryCode'], time() + 3600);
```

### Todo

[](#todo)

- Allow devs not to pass an IP address (since it isn't require by the API)
- The `->getIpAddress()` function [probably needs additional testing](http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php)

### License

[](#license)

This is open-sourced software licensed under the [MIT license](http://beingtomgreen.mit-license.org/).

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.1% 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.

### Community

Maintainers

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

---

Top Contributors

[![BeingTomGreen](https://avatars.githubusercontent.com/u/1327919?v=4)](https://github.com/BeingTomGreen "BeingTomGreen (39 commits)")[![danio1024](https://avatars.githubusercontent.com/u/1771123?v=4)](https://github.com/danio1024 "danio1024 (2 commits)")

### Embed Badge

![Health badge](/badges/danio1024-ip-user-location/health.svg)

```
[![Health](https://phpackages.com/badges/danio1024-ip-user-location/health.svg)](https://phpackages.com/packages/danio1024-ip-user-location)
```

###  Alternatives

[kaufmanndigital/gdpr-cookieconsent

A ready-to-run package, that integrates an advanced cookie consent banner into your Neos CMS site.

2540.7k](/packages/kaufmanndigital-gdpr-cookieconsent)[selective/transformer

A strictly typed array transformer with dot-access, fluent interface and filters.

3817.8k1](/packages/selective-transformer)[derhansen/sf_banners

Banner-Management Extension based on Extbase and Fluid. Loads banners asynchronously using JavaScript.

1144.5k](/packages/derhansen-sf-banners)[martin/wn-forms-plugin

Create easy (and almost magic) AJAX forms

212.3k](/packages/martin-wn-forms-plugin)

PHPackages © 2026

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