Coder Social home page Coder Social logo

interactiondesignfoundation / laravel-geoip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from torann/laravel-geoip

8.0 0.0 1.0 392 KB

🌎 Determine the geographical location of website visitors based on their IP addresses.

Home Page: https://interactiondesignfoundation.github.io/laravel-geoip/

License: BSD 2-Clause "Simplified" License

PHP 100.00%
laravel laravel-package geolocation laravel-geolocation maxdb maxmind maxmind-geoip ipapi ipgeolocation ipdata ipfinder

laravel-geoip's Introduction

GeoIP for Laravel

run-tests Latest Stable Version Total Downloads Type coverage Psalm error level

Determine the geographical location and currency of website visitors based on their IP addresses.

About this fork

We have forked torann/geoip as it’s almost not actively maintained anymore. This fork works with modern PHP versions only (8.0+), maintained and includes additional features:

  • Support modern Laravel and PHP versions
  • Better types (native and PHPDoc)
  • Safer file and network functionality
  • Ability to prefix cache keys (cache_prefix config option)
  • Ability to change service using GEOIP_SERVICE env var
  • Updated currencies
  • More predictable exceptions

But don’t worry, we are following SemVer 2.0. The package migration is straightforward.

Installation

From the command line run:

composer require interaction-design-foundation/laravel-geoip

Publish the configurations

Run this on the command line from the root of your project:

php artisan vendor:publish --provider="InteractionDesignFoundation\GeoIP\GeoIPServiceProvider" --tag=config

Configuration

Quick breakdown of the two main options in the configuration file. To find out more simple open the config/geoip.php file.

Service Configuration

To simplify and keep things clean, all third party composer packages, which are needed for a service, are installed separately.

For further configuration options, checkout the services page.

Caching Configuration

GeoIP uses Laravel’s default caching to store queried IP locations. This is done to reduce the number of calls made to the selected service, as some of them are rate limited.

Options:

  • all all locations are cached
  • some cache only the requesting user
  • none caching is completely disabled

Usage

There are few options to use the package:

  • geoip() helper function
  • InteractionDesignFoundation\GeoIP\Facades\GeoIP facade
geoip()->getLocation('27.974.399.65'); // Get the location from the provided IP.
geoip()->getClientIP(); // Will return the user IP address.

Example of a Location object:

\InteractionDesignFoundation\GeoIP\Location {[
    'ip'           => '1.1.1.1',
    'iso_code'     => 'US',
    'country'      => 'United States',
    'city'         => 'New Haven',
    'state'        => 'CT',
    'state_name'   => 'Connecticut',
    'postal_code'  => '06510',
    'lat'          => 41.28,
    'lon'          => -72.88,
    'timezone'     => 'America/New_York',
    'continent'    => 'NA',
    'currency'     => 'USD',
    'default'      => false,
]}

Location class implements \ArrayAccess interface, means you can access properties of the Location object using both object and array access:

$location = geoip()->getLocation();

$city = $location->city;
// The same as:
$city = $location['city'];

Artisan

Some services require downloading and use a local database to detect Location by IP address. There is a console command to download/update database:

php artisan geoip:update

Some cache drivers offer the ability to clear cached locations:

php artisan geoip:clear

Changelog

Please see Releases for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Contributions

Many people have contributed to the project since its inception.

Thanks to:

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.