Jump to content

GeoLocation lookups via a smarty plugin


robb3369

Recommended Posts

Here is a smarty plug I wrote to help with the Signup Forms State Dropdown started by Matt. This plugin requires the MaxMind GeoIP API and both the GeoLite Country and GeoLite City databases be installed and configured on your server. You can check via PHPInfo or with this snippet of code (on your server, not your local machine):

<?php
$record = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
if ($record) {
   print_r($record);
}
?>

 

While the results are usually not perfect :cry: - they are close enough (we primarily use the country and state):

continent_code = NA
country_code = US
country_code3 = USA
country_name = United States
region = NC
city = Holly Springs
postal_code = 27540
latitude = 35.6021995544
longitude = -78.8786010742
dma_code = 560
area_code = 919

 

Create a file called function.geoip.php in the /WHMCS/includes/smarty/plugins/ directory:

<?php
/*
* Smarty plugin
* File:       function.geoip.php
* Type:       function
* Name:       geoip
* Purpose:    pulls data from local geopip database and outputs a response
* References: http://php.net/manual/en/function.geoip-record-by-name.php
*             http://www.maxmind.com/app/c
* Parameters: "continent_code" -- Two letter continent code (as of version 1.0.4 with libgeoip 1.4.3 or newer)
*             "country_code" -- Two letter country code (see geoip_country_code_by_name())
*             "country_code3" -- Three letter country code (see geoip_country_code3_by_name())
*             "country_name" -- The country name (see geoip_country_name_by_name())
*             "region" -- The region code (ex: CA for California)
*             "city" -- The city.
*             "postal_code" -- The Postal Code, FSA or Zip Code.
*             "latitude" -- The Latitude as signed double.
*             "longitude" -- The Longitude as signed double.
*             "dma_code" -- Designated Market Area code (USA and Canada only)
*             "area_code" -- The PSTN area code (ex: 212)
* @author     Rob Breault <robb3369@gmail.com>
* @version    1.0
* @param      array
* @param      smarty
* @return     string
*/
function smarty_function_geoip($params, &$smarty)
{
   if(!isset($params['field'])) {
       $smarty->trigger_error("geoip: field not set");
       return;
   }
   $return = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
   if ($return) {
       return $return[$params['field']];
   }
}
?>

You can access the GeoIP data using the following smarty calls:

{geoip field="continent_code"}
{geoip field="country_code"}
{geoip field="country_code3"}
{geoip field="country_name"}
{geoip field="region"}
{geoip field="city"}
{geoip field="postal_code"}
{geoip field="latitude"}
{geoip field="longitude"}
{geoip field="dma_code"}
{geoip field="area_code"}

Putting it all together, here is how we modified the state dropdown (from the above mentioned forum post) to select our vistors state. From this:

<input type="text" name="state" size="25" value="{$clientstate}" />

To this:

<input type="text" name="state" size="25" value="{if $clientstate}{$clientstate}{else}{geoip field="region"}{/if}" id="state" /><div id="statedropdown"></div>

I know this can be done other ways with xyz site and jquery lookups... but this is how we do chose to do this.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated