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.


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 two options: manual and weekly automatic update of binary database. Maxmind updates the DB every Tuesday and Smart IP automatically download every Wednesday (if automatic update is selected). In manual update option, the binary file must be downloaded here (for licensed version) and for lite version Maxmind binary file can be downloaded here. The downloaded binary (.dat) file must be uploaded manually to your server.
  • Maxmind's Apache module mod_geoip... read more.
  • Paid GeoIP Legacy Web Services. Need to purchase developer key in using this.
  • Paid GeoIP2 Precision Web Services. Needs user ID and license 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.


  • 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)


  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.
  5. If your server is using reverse proxy (like Varnish or nginx), please add the followings codes to your settings.php:
      // Tell Drupal that we are behind a reverse proxy server
      $conf['reverse_proxy'] = TRUE;
      // List of trusted IPs (IP numbers of our reverse proxies)
      $conf['reverse_proxy_addresses'] = array(
  6. For Drupal multi sites, the Smart IP module of each can be setup to lookup to one GeoIP database path. Check this article for instructions: Using GeoIPLookup CLI tool's GeoIP database as Smart IP's data source.

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.


Using direct $_SESSION variable:

    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):

    $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');

Using direct $_SESSION variable:

    if ($_SESSION['smart_ip']['location']['country_code'] == 'UK') {
      $_SESSION['smart_ip']['location']['country_code'] = 'GB';

Using smart_ip_session_set() function (Recommended):

    $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...
Webform Smart IP

This project creates a new Webform field type called 'smart_ip' that uses the Smart IP module to provide additional data about the form submitter in hidden fields. The data will show up on the Results/Submissions admin pages, but will be hidden on the form submission. read more...

EU Cookie Compliance
This module intends to deal with the EU Directive on Privacy and Electronic Communications that comes into effect in the UK on 26th May 2012. read more...

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.
A Drupal module displaying Islamic prayer times in a block. read more...


Smart IP is being developed by Roland Michael dela Peña (arpeggio) of Webfoobar. 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...

Practical application

Project Information