Create Simple GeoIP


Getting country base on ip address is made easy with maxmind databases. You can download them here. You download GeoLite2-City.mmdb and place it in the location in the sample directory below. Recommended installation is via composer

Files Needed

These are the files present in the diretory. vendor directory is generated from composer along with those composer.lock, composer.json. We are going to create index.php and maxmind.php

  • vendor
    • composer
      • some maxmind files here..
    • geoip2
      • some maxmind files here..
    • maxmind
      • some maxmind files here..
    • maxmind-db
      • GeoLite2-City.mmdb
  • composer.json
  • composer.lock
  • composer.phar
  • index.php
  • maxmind.php

Download Composer

Run this command in the root directory of your project.


curl -sS https://getcomposer.org/installer | php
You should now have the file composer.phar in your project directory.

Install Dependencies

Run in your project root:
    php composer.phar require geoip2/geoip2:~2.0

You should now have the files composer.json and composer.lock as well as the directory vendor in your project directory. If you use a version control system, composer.json should be added to it.

Require Autoloader

After installing the dependencies, you need to require the Composer autoloader from your code:
    require 'vendor/autoload.php';
Let's create index.php file

<html>
    <head>
        <title>Simple GeoIP</title>
        <style type="text/css">
        .container {
            width: 970px;
            padding: 0 15px;
            margin-left: auto;
            margin-right: auto;
        }
        #ipAddress {
            border: 1px solid #888;
            height: 42px;
            padding: 8px 10px;
            width: 50%;
        }
        </style>
    </head>
    <body>
    <div class="container">
        <h1>Get Country Via IP Address</h1>
        <input type="text" id="ipAddress" placeholder="enter ip address"><br><br>
        <div id="result"></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#ipAddress').keyup(function() {
            var $ip = $(this).val();
            $.post(
                'maxmind.php',
                {ip:$ip },
                function(data) {
                    $('#result').html(data);
                }
            );
        });
    })
    </script>
    </body>
</html> 
   
We need jquery for this example. Here is the sample output:


Now let's create the maxmind.php file
   
<?php
 require_once 'vendor/autoload.php';
 use GeoIp2\Database\Reader;

 if ($_POST['ip']) {
  $ip = $_POST['ip'];
  if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {
      echo "<strong>IP ADDRESS:</strong> " . $ip . " is a valid.<hr>";
      // This creates the Reader object, which should be reused across lookups.
   $reader = new Reader('vendor/maxmind-db/GeoLite2-City.mmdb');

   // Replace "city" with the appropriate method for your database, e.g.,
   // "country".
   $record = $reader->city($ip);

   echo "<pre>";
   echo "<p><strong>ISO CODE:</strong> ".$record->country->isoCode."</p>";
   echo "<p><strong>COUNTRY NAME:</strong> ".$record->country->name."</p>";
   echo "<p><strong>CITY NAME:</strong> ".$record->city->name."</p>";
   echo "<p><strong>POSTAL CODE:</strong> ".$record->postal->code."</p>";
   echo "<p><strong>LATITUDE:</strong> ".$record->location->latitude."</p>";
   echo "<p><strong>LONGITUDE:</strong> ".$record->location->longitude."</p>";
  } else {
      echo "$ip is not a valid IP address";
  }
 }
?>
    
I added just simple ip validation. Download database here: GeoLite2 City, extract the file and put GeoLite2-City.mmdb file inside 'vendor/maxmind-db/'. Now when you put the IP Address it will get the country and other data. See output below:
SHARE
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment