Archive for the ‘Programming’ category

Four free Geolocation Methods

August 22nd, 2009

Geolocation or Geo-Targeting is a method of identifying a visitor’s location in the world. You can use this information for anything as simple as greeting a visitor in their native language to automatically redirecting visitors to valid affiliate offers for the visiting demographic. This article takes a look at four different services that offer geolocation for free. This article will focus primarily on retrieving the visitor’s country code.

Maxmind GeoLite Country

Touting a 99.5% data accuracy is the GeoLite Country database allowing you to lookup a visitor’s country locally on your server. The database file is updated at the first of each month. The benefit with a local binary database is that lookups are performed right at the server and as a result may be faster and easier to cache than hitting a remote server you have no control over. However performing a large number of queries maybe cause some strain on your server if you have a cheaper machine or use shared hosting.

The easiest way to use the GeoLite country database is to download the binary format along with the file from the Maxmind PHP API. Once the GeoIP.dat and files are uploaded onto the server, the following PHP code will retrieve the visitor’s country code.

$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$addr = $_SERVER["REMOTE_ADDR"];
$country_code = geoip_country_code_by_addr($gi, $addr);	

Other APIs include C, Perl, Javascript, Python, C#, Ruby and Java examples, as well as an Apache Module and Microsoft COM Objects.


One of the more flexible remote service is This one provides the most information from a single line PHP code, but requires a connection to the server for every request (I recommend caching or using cookies for repeat visitors).

PHP Example:

$fetch = unserialize(fetch_url(''.$_SERVER['REMOTE_ADDR']));
$geo_code = $fetch['geoplugin_countryCode'];

You can also easily grab the city name using the ‘geoplugin_city’ key in the returned array. Other options supported are Javascript, JSON and XML . offers the simplest way to retreive a country code for a given IP address.

$geo_code = trim(fetch_url("".$_SERVER['REMOTE_ADDR']));
While not as straight forward as HostIP or GeoPlugin, provides a backup server in the event the first request fails.

$fetch = fetch_url("".$_SERVER['REMOTE_ADDR']."&output=xml");
	$fetch = fetch_url("".$_SERVER['REMOTE_ADDR']."&output=xml");
$fetchb = new SimpleXMLElement($fetch);
if (!$fetchb) 
	$geo_code = "";
	$geo_code = $fetchb->CountryCode;

There you have it, four different geolocation services, depending on your needs some may perform better than others, for high traffic sites the Maxmind solution is likely the best one short of paying for a monthly service.

AJAX with JQuery

August 22nd, 2009

AJAX stands for Asynchronous Javascript and XML, though most people would associate the term with dynamic loading of content without refreshing the browser. I notice that even today a lot of people starting out with AJAX still use the XMLHttpRequest object from scratch that has been around forever.

Traditionally retrieving content with such a method would require no less than something like this:

<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp!=null)
  alert("Your browser does not support XMLHTTP.");
function state_Change()
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    alert("Problem retrieving data:" + xmlhttp.statusText);
<body onload="loadXMLDoc('test_xmlhttp.txt')">
<div id="T1" style="border:1px solid black;height:40;width:300;padding:5"></div><br />
<button onclick="loadXMLDoc('test_xmlhttp2.txt')">Click</button>

But with the use of many popular frameworks such as Prototype, JQuery, Mootools, Dojo and others AJAX doesn’t have to be a huge mess for a task so simple. Not only would you get simplicity but better cross browser compatibility as the frameworks take a lot of quirks out of the latest browsers. If a new browser comes out, you can usually just update the framework file to keep all your code compatible.

Here is an example of how the above code could be made simpler with JQuery:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="en" lang="en">

Defining a DOCTYPE is very important when dealing with a javascript framework in order to ensure maximum compatibility with most browsers (especially Internet Explorer). I tend to work in xHTML 1.0 99% of the time because I find the best luck with it in a cross browser market. It’s a much stricter document type than HTML 4.01 Transitional, but it tends to work much better.

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
     $(document).ready(function() {
          $("button.trigger").bind("click", function(e){
<div id="T1" style="border:1px solid black;height:40;width:300;padding:5"></div><br />
<button class="trigger">Click</button>

As you can see, not only does JQuery make AJAX loading easy, the selectors are CSS1-3 based making element selection quite easy as well. For folks needing a bit more control on the return data JQuery also has the get(), post() and ajax() functions. More information regarding these functions can be found here : API/1.3/Ajax

Paypal IPN with PhP

August 19th, 2009

If you’re a freelance coder you most likely have a PayPal account. One of the most useful feature provided by PayPal for anyone looking to automate their ordering process is the Instant Payment Notification. This guide will show you how to utilize IPN with a PayPal ‘Buy Now’ button and PHP. Additional tips to further secure your ordering process are also discussed.

What is IPN?

In a nutshell IPN is PayPal method of instantly notifying your server of a payment. This can either be setup globally for all transactions on your account, or can be provided for a specific button or subscription.

Merchant Services

The IPN Process:

  1. Paypal sends details of the transaction to your server at the provided url.
  2. Your script compiles the information provided and sends it to Paypal’s verification server.
  3. Paypal’s server will either verify the information as valid, or will reject it as invalid.
  4. If considered a valid transaction, process the information as needed, otherwise discard and ignore.

Creating a Payment Button

Once logged into PayPal you should direct your attention to the Merchant Service.

The “Buy Now Button” will be sufficient for a one-time-purchase of an electronic or tangible product. The button can be setup as a simple buy now button for a single purchase with no options, or can be setup with drop down for product choices or other options. Either way there is two key settings you will want to use when creating your button.

Secure Merchant ID

Select Secure Merchant ID as means of identifying the account as opposed to an email address. This will not only help prevent automated spam attempts but will also help prevent fake transactions (more on that later).

IPN LocationYou will also need to add the following line to the advanced option. The url will be the destination notified in the event of a payment or related transaction. Normally you don’t want to call it something as obvious as ipn.php in the root of your site, rather bury the script in a folder and give it some other name such as txn1.php.

Tracking OptionIf you have multiple options that you will want to verify in your IPN script you can get PayPal to send you the specific Item ID by turning on the tracking option for the button:

Once you have created your button and generated the code to be used on the site you will want to setup your IPN script to save the necessary transactions.