Giter Site home page Giter Site logo

ip2country's Introduction

Laravel Country Lookup by IP Address

Downloads License Version TravisCI

Laravel package to lookup the country associated with an IPv4 address. Developed with an eye to keeping it as lightweight and lookups as fast as possible. Creates and populates a local database table, so there are no external requests being made during runtime.

This package includes GeoLite data created by MaxMind, available from http://www.maxmind.com. The updated free downloadable database is released the first Tuesday of each month. I'll attempt to ensure to update this package each time.

How to Install

Require the package with composer.

composer require "smalldogs/ip2country"

Create and populate the database lookup table.

php artisan migrate --package="smalldogs/ip2country"

Lastly, add the service to your providers array in app/config/app.php

'providers' => array(
        'Smalldogs\Ip2Country\Ip2CountryServiceProvider',
        //[...]
);

How to Use

// Returns the 2 letter country code for the user, eg: 'US'
$myCountryCode = Ip2Country::get();

// Returns the full name of the country, eg: 'United States'
$myCountryName = Ip2Country::getFull();

// Get the country for someone other than user on the page
$someonesIpAddress = '192.168.0.1';
$someonesCountryCode = Ip2Country::get($someonesIpAddress);

Configuration

By default, if the IP address is not found in the lookup table, it will return 'US' as a country code, and 'United States' as a country name. You can customize this in the config.

php artisan config:publish smalldogs/ip2country

Then navigate to app/config/packages/smalldogs/ip2country/config.php.

return array(
    // If IP address is not found, what country is returned
    'default_country_code' => 'US',
    'default_country_name' => 'United States',
    
    // Since our data will change, at most, once a month. Cache the Ip lookup for a day
    // Default: 1 day. Set to 0 or null to disable.
    'cache_results' => 60 * 24
);

If you don't want anything returned if the IP address is not found, simply set each of these to => null

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.