2017 © Pedro Pelรกez
 

library laravel-ip2location

Use the free Ip2Location LITE database within Laravel

image

dynamiccarrots/laravel-ip2location

Use the free Ip2Location LITE database within Laravel

  • Friday, February 24, 2017
  • by dynamiccarrots
  • Repository
  • 0 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Laravel-Ip2Location

Ip2Location provides a free non-intrusive database to help you identify your visitors geographical location., (*1)

The data you receive from a users IP Address will depend on what database size you download from Ip2Location. The largest of these consists of Country, Region, City, Latitude, Longitude, Zipcode and Timezone., (*2)

Only download the database that you need, the large databases can have over 4 million rows. The smallest one has about 160K rows., (*3)

As this package uses the LITE/free version of Ip2Location you are not guaranteed 100% accuracy., (*4)

Installation

Download Ip2Location Database

Visit this page and download one of the DB files as a CSV, these range from DB1 to DB11. http://lite.ip2location.com/, (*5)

You will need to create a folder in the database directory called "ip2location" and store the CSV in here with the name "ip2location.csv"., (*6)

You should have the following structure., (*7)

    โ”œโ”€โ”€ database/
    โ”‚   โ”œโ”€โ”€ ip2location/
    โ”‚   โ”‚   โ”œโ”€โ”€ ip2location.csv

Migrations

Create a new migration file in laravel by running the following command., (*8)

# php artisan make:migration create_ip2location_table

Copy the following migration in to the newly created migration file., (*9)

   /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ip2location', function (Blueprint $table) {
          $table->integer('ip_from')->unsigned();
          $table->integer('ip_to')->unsigned();
          $table->char('country_code', 2);
          $table->string('country_name', 64)->nullable();
          $table->string('region_name', 128)->nullable();
          $table->string('city_name', 128)->nullable();
          $table->double('latitude')->nullable();
          $table->double('longitude')->nullable();
          $table->string('zip_code', 30)->nullable();
          $table->string('time_zone', 8)->nullable();
          // Setting index
          $table->index('ip_from', 'idx_ip_from');
          $table->index('ip_to', 'idx_ip_to');
          $table->index(['ip_from', 'ip_to'], 'idx_ip_from_to');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('ip2location');
    }

Run the migration., (*10)

# php artisan migrate

Create update command

Due to the fact IP address ranges get moved around from ISP to ISP all the time. We need to update our database on a monthly basis if you want to stay up to date., (*11)

We need to have a command that will allow us to change the ip2location.csv file and use its data to refresh our database., (*12)

Run the following command., (*13)

# php artisan make:command RefreshIp2Location

This will create a new file in app/Console/Commands called RefreshIp2Location.php., (*14)

Replace the code in this new file with this., (*15)

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Ip2Location\Ip2Location;

class RefreshIp2Location extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ip2location:refresh';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Refresh the Ip2location table with file /database/ip2location/DB11.csv';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        Ip2Location::refreshDatabase();
        return;
    }
}

Register the new command in /app/Console/Kernel.php by adding RefreshIp2Location::class to the $commands array, this will allow us to run this from the CLI., (*16)

    protected $commands = [
        RefreshIp2Location::class
    ];

Populate the database

Now we have a database set up with the correct table and the csv file stored in the right directory we can run the command to populate the database with the ip2location data., (*17)

Run this command., (*18)

# php artisan ip2location:refresh

Use Examples

Return the Country Code of a IP address

Provide your own IP Address and Ip2Location will convert this to a Country Code., (*19)

echo Ip2Location::lookUpIpLocation('8.8.8.8')->country_code

Return the Country Code client

Return the Country Code of the client, (*20)

echo Ip2Location::getClientsLocation()->country_code

The Versions

24/02 2017

dev-master

9999999-dev

Use the free Ip2Location LITE database within Laravel

  Sources   Download

The Requires

 

23/02 2017

0.0.4

0.0.4.0

Use the free Ip2Location LITE database within Laravel

  Sources   Download

The Requires

 

23/02 2017

1.0.0

1.0.0.0

Use the free Ip2Location LITE database within Laravel

  Sources   Download

The Requires

 

23/02 2017

0.0.3

0.0.3.0

Use the free Ip2Location LITE database within Laravel

  Sources   Download

The Requires

 

23/02 2017

0.0.2

0.0.2.0

Use the free Ip2Location LITE database within Laravel

  Sources   Download

The Requires

 

23/02 2017

0.0.1

0.0.1.0

Use the free Ip2Location LITE database within Laravel

  Sources   Download

The Requires

  • php >=5.3.0