Overview

Smart IP identify visitor's geographical location (longitude/latitude), country, region, city and postal code based on the IP address of the user. These information will be stored at session variable ($_SESSION) with array key 'smart_ip' and Drupal $user->data object with array key 'geoip_location' of the user but optionally it can be disabled (by role) at Smart IP admin page. Other modules can use the function smart_ip_get_location($ip_address) that returns an array containing the visitor's ISO 3166 2-character country code, longitude, latitude, region (FIPS), city and postal code. It provides a feature for you to perform your own IP lookup and admin spoofing of an arbitrary IP for testing purposes.

Features:

  • Eight data source options available: local database with data from parsed Maxmind CSV, paid Maxmind GeoIP Web Services, Maxmind binary file database (thanks to jbulcher), Maxmind's Apache module mod_geoip, IP2Location binary file, IPInfoDB.com service, X-GeoIP-Country: XX header (thanks to jp.stacey) and Cloudflare IP Geolocation.
  • Maxmind's Apache module mod_geoip and X-GeoIP-Country as fallback for the current visitor's geolocation info if the data source of Smart IP returns empty.
  • Monthly auto-update of Maxmind CSV for local database source
  • Weekly auto-update of Maxmind binary format file (for paid version)
  • Visitor’s geolocation block available (Device Geolocation module)
  • Geolocate users by role (see details: http://drupal.org/node/990546)
  • Update users' geolocation info based on defined time interval (at "Frequency of user's geolocation checking" field). Useful to get the updated users' geolocation even they moved from one place to another.
  • Geolocate users to specific pages (see details: http://drupal.org/node/1069440) and with timeout that will prompt users for geolocation (see details: http://drupal.org/node/1061264)
  • User's geolocation update using AJAX, useful for webpages or sites that are cached
  • Token support (For Drupal 7 only. Thanks to bgilhome)
  • Geolocation Historical Data Records (For Drupal 6 only and still in progess see http://drupal.org/node/1027158)
  • Exposes Smart IP visitor's location details to Views field (coordinates, country, ISO 3166 2-character country code, region, region code (FIPS), city and zip) and filter (country, ISO 3166 2-character country code, region, region code (FIPS), city and zip). (For Drupal 6 only)
  • Integration with Openlayers Proximity via Views filter (For Drupal 6 only. See details: http://drupal.org/node/1207582 - sponsored by nyleve101)
  • Integration with Rules module. (For Drupal 7 only. Thanks to klausi)
  • Supports Session Cache API.
  • Maxmind GeoIP Web Services, available for Country Web Service (Thanks to echataig) and City/ISP/Organization Web Service (Thanks to namli and duntuk)
  • Smart IP with a Maxmind GeoIP City Database from a paid account read steps (documented by jbulcher)

Data Sources

  • Maxmind's database is the source of Smart IP database that makes the association between IP address and geographical location (longitude/latitude), region, city and postal code. It can be found at http://www.maxmind.com/app/geolitecountry it has two versions: a very accurate and up to date payable version and a not quite accurate free lite version. Smart IP downloads and process the CSV files (GeoLiteCity-Location.csv and GeoLiteCity-Blocks.csv) to store to Smart IP database. An optional once a month (Maxmind updates its database every first day of a month) automatic update of the Smart IP database is provided or it can be manually updated at Smart IP admin page. The database of Maxmind is very huge, the two CSV files size is about 150MB and the size when stored to SQL database is about 450MB with almost 5 million rows and about 600MB additional database space for temporary working table for Smart IP database update. The process of downloading the archived CSV files from Maxmind's server, extracting the downloaded zip file, parsing the CSV files and storing to the database will took more or less eight hours (depends on server's speed). It uses the batch system process. If interrupted by an unexpected error, it can recover from where it stopped or the administrator can manually continue the broken process at Smart IP admin page.
  • Maxmind binary format database. Smart IP supported paid and lite versions. Paid version needs license key purchased from maxmind.com. Maxmind's binary database uses "a custom binary format to maximize lookup speed", made accessible via their custom API. The binary database is roughly 40MB, and will be downloaded and extracted automatically at Smart IP admin page. It has an option of weekly automatic update of binary database. Maxmind updates the DB every Tuesday, and Smart IP download every Wednesday. Lite version Maxmind binary file can be downloaded here and the downloaded GeoLiteCity.dat must be uploaded manually to your server.
  • Maxmind's Apache module mod_geoip... read more.
  • Paid Maxmind GeoIP Web Services (Only available for Country and City/ISP/Organization Web Service). Need to purchase developer key in using this.
  • IP2Location binary file which is available for download at IP2Location website (need to register - its free). It comes in lite and paid versions. This binary file needs to be uploaded to site's server and configure the uploaded path at Smart IP admin page. Caching such as "Memory cache" and "Shared memory" are available for accelerate lookup speed.
  • Another source of Smart IP is the IPInfoDB.com service which uses IP2Location, in this case IPInfoDB.com will handle database resource load instead of your server's database. By default the use of IPInfoDB.com service as source is enabled. If IPInfoDB.com is desired to handle database resource load, it can be configured at Smart IP admin page settings.
  • X-GeoIP-Country: XX header, set by e.g. nginx via $_SERVER['HTTP_X_GEOIP_COUNTRY'] variable.
  • Cloudflare IP Geolocation: your website must be using Cloudflare CDN and "IP Geolocation" option must be enabled at your Cloudflare settings. Cloudflare adds CF_IPCountry header to all requests to your site, accessed via $_SERVER['HTTP_CF_IPCOUNTRY'] variable.

Requirements

  • IPInfoDB.com service API key which requires a registration at http://ipinfodb.com/register.php
  • Device Geolocation (Optional - for more detailed visitor's geolocation using client device location source)
  • Session Cache API (for sites that uses caching engine or web accelerator like Varnish)

Installation

  1. Copy the extracted smart_ip directory to your Drupal sites/all/modules directory.
  2. Login as an administrator. Enable the module at http://www.example.com/?q=admin/modules
  3. Set your private file system path at http://www.example.com/?q=admin/config/media/file-system
  4. Configure/update Smart IP database/lookup an IP at http://www.example.com/?q=admin/config/people/smart_ip.

Note: The Smart IP database is empty upon initial installation of this module. Either manually update the Smart IP database at admin page or wait for the cron to run and update Smart IP database automatically for you.

Configuration / Cache Performance Note

Be aware that this module adds location data to the session of any roles you choose to geolocate. If you enable geolocation for anonymous users, all of your visitors will bypass the page cache.

If you wish to display geotargted content to anonymous users, while still maintaining the page cache for your general content, consider the Ajax Blocks project, paired with a custom block that fires off the geolocation code directly.

Session get and set functions

As support for Session Cache API, Smart IP implemented get and set functions accessing storage of visitor's geographical location. For developers, we are strongly suggest to use these get and set functions in your codes. Below are the sample codes usage.

Get

Using direct $_SESSION variable:

  <?php
   
if ($_SESSION['smart_ip']['location']['country_code'] == 'PH') {
     
drupal_set_message(t('You are in the Philippines.'), 'status');
    }
 
?>

 

Using smart_ip_session_get() function (Recommended):

  <?php
    $smart_ip_session
= smart_ip_session_get('smart_ip');
    if (
$smart_ip_session['location']['country_code'] == 'PH') {
     
drupal_set_message(t('You are in the Philippines.'), 'status');
    }
 
?>

 

Set

Using direct $_SESSION variable:

  <?php
   
if ($_SESSION['smart_ip']['location']['country_code'] == 'UK') {
     
$_SESSION['smart_ip']['location']['country_code'] = 'GB';
    }
 
?>

 

Using smart_ip_session_set() function (Recommended):

  <?php
    $smart_ip_session
= smart_ip_session_get('smart_ip');
    if (
$smart_ip_session['location']['country_code'] == 'UK') {
     
$smart_ip_session['location']['country_code'] = 'GB';
     
smart_ip_session_set('smart_ip', $smart_ip_session);
    }
 
?>

 

Modules that uses Smart IP:

Geolocate Logs
Provides geographical location for Drupal log event's hostname (Recent log messages and Recent hits) and whois lookup for hostname using any free external site offering whois service. read more...
Weather

This module uses METAR (METeorological Aerodrome Report) data to display current weather conditions from anywhere in the world. read more...

Weather block

The module provides support for Yahoo Weather, World Weather Online and weather.com weather forecasts. read more...

IP Geolocation Views and maps
  1. a Views mapping engine, combined with
  2. a set of visitor location retrieval technologies based on HTML5 (GPS) and/or IP address lookups. read more...
Get Locations

Provide Google maps API version 3 enabled maps on which to display markers of locations found in location-enabled content-types.

These can be displayed per node, eg "/getlocations/node/xxx" will display all the locations associated with that node. They can also be displayed per content-type, so if your content-type has a machine name 'venue' you can show them all with "/getlocations/type/venue". read more...

Mapstraction CCK IP

This sub-module of Mapstraction CCK provides the integration between the Smart IP module and Mapstraction CCK, allowing to center maps or include geotags in the web user location, retrieved from his IP. read more...

IP to Locale
Provides IP lookup and redirection services for Drupal sites. read more...
IP Block
Block user access based on their IP address. read more...
Adhan
A Drupal module displaying Islamic prayer times in a block. read more...

Customizations

Smart IP is being developed by Roland Michael dela Peña (arpeggio) of Web->proton. The author may be contacted for paid customisations of this module, Drupal consultation/installation/optimization or other Drupal-related projects.

Get involve

If you find this module useful, please consider the following:

  • Helping other users that request for a support by sharing what you know is a kind thing to do.
  • Patches are welcome. If you are developer, help fix reported bugs by sharing patches.
  • Want to help but don't have the time, giving donation would help me allocate more time to support new feature request and fix old bugs (Donate here).

Your support is much appreciated. Thank you in advance.

Device Geolocation

Device Geolocation is integrated into this module which gives more detailed visitor's geolocation using client device location source that implements HTML5 or W3C Geolocation API whereas the coordinates are geocoded using Google Geocoding service. More datails...

Live sample application

Dynamic content based on country
Duplika internet solution uses a code snippet for some of their selected contents which display different Price and links based on their visitor's country. Sample code snippet they used:
<?php $smart_ip_session = smart_ip_session_get('smart_ip'); ?>
<?php if ($smart_ip_session['location']['country_code'] == 'AR'): ?>
  <p>Argentina HTML content specific</p>
<?php elseif ($smart_ip_session['location']['country_code'] == 'UY'): ?>
  <p>Uruguay HTML content specific</p>
<?php else: ?>
  <p>Fallback default content.</p>
<?php endif; ?>

Note: The $smart_ip_session['location']['country_code'] is the ISO_3166‐1_alpha‐2 which can be found the list of country codes at http://en.wikipedia.org/wiki/ISO_3166‐1_alpha‐2.

Project Information

Downloads