Affiliate Hacks #1 – Use Javascript GeoIP To Insert Location on Landing Pages

Ever hit a website or landing page that says “We Have (5) New Updates in Los Angeles, California!” and wonder how they detected your location & displayed it? The answer and application is probably more simple than you think.

Using the MaxMind GeoIP Script

Thanks to the folks at MaxMind, the script to detect location based on IP address is already made, and free. First we call the script right after our opening body tag:

<body>
<script type="text/javascript" src="https://j.maxmind.com/app/geoip.js"></script>

After that, you simply copy and paste the below Javascript code when you want to print out a certain location:

<script>document.write(geoip_country_code());</script> //Writes Country Code (e.g. US)
<script>document.write(geoip_country_name());</script> //Writes Country Name (e.g. United States)
<script>document.write(geoip_city());</script> //Writes City (e.g. Manhattan)
<script>document.write(geoip_region());</script> //Writes Region (e.g. NY)
<script>document.write(geoip_region_name());</script> //Writes Region Name (e.g. New York)

That’s all there is to it! If you’d like to reference these easily and aren’t going to memorize them, just comment them out in your code and copy/paste where needed:

<body>
<script type="text/javascript" src="https://j.maxmind.com/app/geoip.js"></script>
<!--
Country Code: <script>document.write(geoip_country_code());</script>
Country Name: <script>document.write(geoip_country_name());</script>
City: <script>document.write(geoip_city());</script>
Region: <script>document.write(geoip_region());</script>
Region Name: <script>document.write(geoip_region_name());</script>
-->

<p>We Found Cheap Auto Insurance Rates in <script>document.write(geoip_region_name());</script></p>

</body>

Alternate: Using the PHP Version

The script above is simple and works great, but it calls MaxMind’s hosted javascript files. If MaxMind happens to be running slow, or shuts down, your GeoIP will stop working. The alternative (and my preferred way) is to use PHP to make these lookups on your own server. How can this be done?

Step 1: Download Source Files

Download the source files above and upload them to your server.

Step 2: Call the GeoIP Script and Set the City

Next, add the following code to the very top of your page:

<?php
require_once('/server/path/to/geoipcity.inc');
$gi = geoip_open('/server/path/to/GeoLiteCity.dat', GEOIP_STANDARD);
$location = GeoIP_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$city = $location->city;
$state = $location->region;
?>

What we’ve done here is loaded the script, and set the city to a variable called $city. You can now insert the city into your page like this:

<h1>Welcome to <?php echo $city . ", " . $state ?>!</h1>

That’s all there is to it. Using the PHP version is nice if you’re working with more advanced/dynamic pages. Having the city set to a variable allows you to do all kinds of cool things, like auto detect the City/State/Zip and pre-populate it into host&post forms.

For more information on everything possible with this code, head over to the official MaxMind website.

How to Utilize This Hack

This is a great way to add trust to your landing pages by letting the user know you do business in their area. Some examples of usage might be:

  • “Arizona Auto Insurance Rates Drop 50%!”
  • “There are currently 1,229 singles in Chicago”
  • “Special Alert for Arizona Residents”
  • “Wisconsin native discovers method for making money online”

Get creative and find a way to work these into your own niches!


1 Comment

  1. April 2, 2014

    maxmind is surprised this method is still working (oops)
    seems they may shut it down s

Leave a Comment