User Tools

Site Tools


web_development:maxmind_geoip_api

The following scripts make use of the MaxMind GeoIP Javascript, which is freely usable as long as you adhere to the Terms of Use below.

These scripts may be used to control access to your web site based upon where the user is located.

This process has obvious limitations: in order for it to work, the user must have a javascript-enabled browser. That's not to say this is not a reasonably good control mechanism for your web site; but this is by no means a 100% solution to filtering traffic.

'Note:' please choose one script to run on your site. You cannot run, for example, Deny Access by Region on the same page as Deny Access by City. They would conflict.

License / Terms of Use

In order to be legally permitted to utilize this code, you must adhere to the following.

The GeoIP Deny Access by Region / Deny Access By City / Get Directions From Google By Current Location scripts are licensed under the [http://creativecommons.org/licenses/by/2.5/ca/ Creative Commons Attribution 2.5 Canada License] under the following terms:

1. This GeoIP Deny Access by Region / Deny Access By City / Get Directions From Google By Current Location script was originally written by Robbie Ferguson - [http://www.Category5.TV www.Category5.TV]

2. This script utilizes MaxMind's free JavaScript City Web Service, available at [http://www.maxmind.com/app/javascript_city www.maxmind.com/app/javascript_city]

3. While they do not specify exactly where (within your site) you must do so, in order to use this code on your website, you must reference them as the source of the geolocation data you are using with this service and provide a link back to their website at www.maxmind.com.

4. It is not required that this link be on every page, or stand out among your content, but at the same time, please don't bury it in obscurity, remembering that the service they provide is free of charge, and your link back is what gives you the legal right to utilize it.

5. A sample link may be:

This site uses geolocation data provided by 
<a href="http://www.maxmind.com" target="_blank">MaxMind</a>.

6. MaxMind may at any time review sites which utilize these scripts to ensure their conformity to its terms of use.

7. MaxMind may give special permission to a third party to waive the above requirement to reference them as the source of the geolocation data and link to their site without seeking permission from Robbie Ferguson on a case-by-case basis, provided that the header of the code which resides in any repository or is used on any site retains section 1 which credits Robbie Ferguson as the original author of the GeoIP Deny Access by Region / Deny Access By City / Get Directions From Google By Current Location script.

8. You may modify the code of the GeoIP Deny Access by Region / Deny Access By City / Get Directions From Google By Current Location script provided that you release your modified code under these same terms of use with yourself as the author of the revision. You may not require the sites which use your revision to credit you on the site itself; however, you may require them to keep the reference to you as the author of the revision intact in the header of your revision and in any subsequent revisions made by others.

9. At its sole discretion, MaxMind reserves the right to deny a revised version of this script the right to include in it MaxMind's JavaScript service.

10. If you modify this script and release it or share it with anyone else, you must allow others to revise it as well, provided that they release it under these original terms without any modification other than that allowed in section 8. For clarification, section 8 allows an author of a revision to add himself as its author to the header and requires any future modifications of his revision to keep that reference intact.

GeoIP Deny Access by Region

In this example, users in Ontario and British Columbia will not be able to access your site; they will be redirected to Google.

GeoIP_Deny_By_Region.js
<!-- GeoIP Deny Access by Region and Redirect Javascript -->
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js">
/*
GeoIP Deny Access by Region and Redirect Javascript 1.0
http://wiki.category5.tv/MaxMind_GeoIP_API
 
API (c) MaxMind - www.maxmind.com - used with permission
 
"GeoIP Deny Access by Region" script by Robbie Ferguson, www.Category5.TV
 
You are free to use and share this script, however this notice must remain intact.
*/
</script>
<script type="text/javascript">
 
/* Configuration Options */
  var region=new Array("ON", "BC") /* Regions to deny access, comma-separated, in quotes */
  var redirect="http://www.google.com/" /* Where to redirect denied traffic */
/* do not edit past this line */
 
Array.prototype.Contains = function(q) {
  for(i in this) {
    if(this[i] === q) return true;
  }
}
if(region.Contains(geoip_region())) window.location = redirect;
</script>
<!-- End of GeoIP Deny Script -->
GeoIP Deny Access by City

In this example, users from Barrie, Ontario or Angus, Ontario will get redirected to Google.

GeoIP_Deny_By_City.js
<!-- GeoIP Deny Access by City and Redirect Javascript -->
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js">
/*
GeoIP Deny Access by City and Redirect Javascript 1.0
http://wiki.category5.tv/MaxMind_GeoIP_API
 
API (c) MaxMind - www.maxmind.com - used with permission
 
"GeoIP Deny Access by City" script by Robbie Ferguson, www.Category5.TV
 
You are free to use and share this script, however this notice must remain intact.
*/
</script>
<script type="text/javascript">
 
/* Configuration Options */
 
/* Cities to deny access, comma-separated, in quotes
   See http://www.maxmind.com/GeoIPCity-534-Location.csv
   Format is City, Region.  Eg. "Barrie, ON"
*/ var city=new Array("Barrie, On", "Angus, ON")
 
/* Where to redirect denied traffic */
  var redirect="http://www.google.com/" 
 
/* do not edit past this line */
Array.prototype.inArray = function(q) {
  for(i in this) { if(this[i].toUpperCase() === q) return true; }
}
var myCity=geoip_city().toUpperCase()
var myRegion=geoip_region().toUpperCase()
if(city.inArray(myCity+", "+myRegion)) window.location = redirect;
</script>
<!-- End of GeoIP Deny Script -->
GeoIP Get Directions from Google by Current Location

Now here's an interesting concept for businesses (for example) who want their web site visitors to be able to get directions to their office easily: a script that detects the visitor's whereabouts, and then generates a Google Map with step-by-step directions to the destination (presumably, the place of business).

In this example, a user will see a button that says “Get Directions”. If clicked, a map is generated by Google Maps which gives the user step-by-step driving directions to the destination you specified in the configuration section–the CN Tower in Toronto in this example. The user never need go through the standard “get directions” procedure; GeoIP is used to determine where the user is driving from.

Place in the <head></head> section of your web site:

GeoIP_Get_Directions_from_Google_by_Current_Location-Head.js
<!-- GeoIP Get Directions from Google by Current Location -->
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js">
/*
GeoIP Get Directions from Google by Current Location Javascript 1.0
http://wiki.category5.tv/MaxMind_GeoIP_API
 
API (c) MaxMind - www.maxmind.com - used with permission
 
"GeoIP Get Directions from Google by Current Location" script by Robbie Ferguson, www.Category5.TV
 
You are free to use and share this script, however this notice must remain intact.
*/
</script>
<script type="text/javascript">
 
/* Configuration Options */
 
/* Set the destination; where we're giving directions to */
var destinationAddress="301 Front St W, Toronto, ON"
var destinationName="CN Tower"
 
 
/* do not edit past this line */
/* Generate map link */
var googlemap="http://maps.google.com/maps?f=d&source=s_d&hl=en&saddr="+geoip_city()+"+"+geoip_region_name()+"&daddr="+destinationAddress+"+("+destinationName+")"
 
var newWindow
function launchMap() {
    newWindow = window.open(googlemap,"","toolbar=yes,scrollbars=yes, resizable=yes,HEIGHT=700,WIDTH=800")
}
</SCRIPT>
<!-- End of GeoIP Google Map Directions Script -->

Place in the <body></body> section of your web site:

GeoIP_Get_Directions_from_Google_by_Current_Location-body.html
<FORM><INPUT TYPE="button" VALUE="Get Directions" onClick="launchMap()"></FORM>
In Conclusion

MaxMind's free JavaScript City Web Service is a useful and interesting tool, helpful for conducting GeoIP-based tasks using easy copy-and-paste javascript code (no need to install additional server modules, etc.), as demonstrated above.

web_development/maxmind_geoip_api.txt · Last modified: 2012/01/26 07:47 by import