Giter Club home page Giter Club logo

ip-user-location's Introduction

IP User Location

A basic PHP wrapper for adding IP based user location detection into your application using the IP Info DB API.

Methods

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

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

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.

Get the user's 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 & query limits

You can get a (free) API key here, 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

License

This is open-sourced software licensed under the MIT license.

ip-user-location's People

Contributors

beingtomgreen avatar

Watchers

James Cloos avatar Miguel Marte Aquino avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.