Okay I've seen many threads with people wanting to easily implement geo targeting on their website for things like Yahoo. Well last night I implemented it on my site and I thought I'd share my methods with everyone else. The strengths of my methodology is that it does not require a database server as it works from a CSV file, It only requires adding one function to your site AND it is FREE.
Step 1) download a current IP "database" from http://software77.net/cgi-bin/ip-country/geo-ip.pl. There is a link to download the database part way down the right hand side of the page.
Step 2) extract the CSV file to a folder on your website.
Step 3) add the following function to your website scripts (I placed it in my "functions.inc" include). I should note that this function is a boiled down version of the script found at http://webnet77.com/scripts/geo-ip/index.html:
Code:
function ipcountrycode($ip){
// convert IP to decimal
$ip=sprintf("%u", ip2long($ip));
// set initial low
$low = 0;
// Open the csv file for reading
$csvfilename="ip2country.csv"; // Change to proper filename and path for IP dataset.
$fp = fopen($csvfilename, "r");
// Set initial high
fseek($fp, 0, SEEK_END);
$high = ftell($fp);
while ($low <= $high) {
$mid = floor(($low + $high) / 2); // C floors for you
//Seek to half way through
fseek($fp, $mid);
// Moves to end of line
if($mid != 0){
$line=fgets($fp);
}
// Read line
$ipdata=fgetcsv($fp,100);
if ($ip >=$ipdata[0] && $ip<=$ipdata[1]){
$low=999999999;
}
elseif($ip >$ipdata[0]){
$low = $mid + 1;
}
else {
$high = $mid - 1;
}
}
fclose($fp);
$line="";
return $ipdata[4];
}
Step 4) Call the above function from the top of the script and store the user's two digit country code in a string that can be referenced where needed:
Code:
$strCountryCode=ipcountrycode($REMOTE_ADDR);
The ipcountrycode function should really only be called once per page load and stored in a string variable for use throughout the script to reduce server overhead. To further reduce server load requirements, the afore mentioned function call can be replaced with the following code, which would set the country code as a cookie allowing the country code to be checked only once every seven days for users who allow cookies:
Code:
// Returns User's country code
//+++++++++++++++++++++++++++++
if(strlen(addslashes($_COOKIE['GeoLocation']))==2){
$strCountryCode=$_COOKIE['GeoLocation'];
}
else{
$strCountryCode=ipcountrycode($REMOTE_ADDR);
// Sets cookie for geo location.
$HostDomain=str_replace("http://","",$_SERVER['HTTP_HOST']);
$SetCookieExpire=mktime() +604800; //Set cookie for one week
setcookie ("GeoLocation", $strCountryCode, $SetCookieExpire, "/", $HostDomain, 0) ;
}
There you have it, a very simple and very clean way to get the country code of your users to do things like geo target ads. Oh make sure to periodically download updates to the IP CSV file so that your targeting remains as accurate as possible.
==EDIT==
2007-03-20:I replaced the function that read the entire file into memory with a function that did a binary compare on the file without reading the entire file into memory. Per rpanella's observations and critique. See post #14
2007-03-23: Fixed some errors that caused country codes not to be returned some times. See post #23
Bookmarks