Category Archives: Web Programming

Geolocation Click Redirection

One of the affiliate offers I participate in redirects visitors to their website to their country specific based websites which is great for the user, not so great for me because when the user is redirected the affiliate cookie is lost and I don’t get credit for the sale. Each country has their own affiliate program so it is possible to get credit for any sales that happen on those country specific sites, but the redirect has to happen before the user visits the main company website where they redirect the user.

While it is possible to create country specific landing pages for each country there is always the chance that a visitor from one of those other countries will still click on a link that would take them to the main company website. To prevent this from happening I have installed a script to check geolocation and redirect the user to the appropriate country specific website instead of them going to the main companies website and being redirected.

Eric Nagel has a PHP script you can use to do something similar, and while his method would work the thought of having to maintain the IP Geolocation database, even through a cron job, just sounds like trouble to me. Not to mention some shared web hosts don’t allow you to run cron jobs or to run a cron job that unzips a file.

The solution I came up with relies on the service Cloudflare. I wrote a post about Cloudflare earlier this year. Cloudflare has this neat little feature called Geolocation where they will add in a server variable that you can access with PHP calle $_SERVER[“HTTP_CF_IPCOUNTRY”]. Once you turn the feature on in Cloudflare all the requests to your Cloudflare enabled website will contain the country code of the user.

All you have to do then is create a PHP redirect script that checks for the Cloudflare variable and redirect the user to the correct link.

Here is a copy of what I use to redirect users.


<?php

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if ($country_code=="UK") {
$link = 'Insert UK Link';
}
elseif ($country_code=="FR") {
$link = 'Insert France Link';
}
elseif ($country_code=="DE") {
$link = 'Insert Germany Link';
}
else {
$link = 'Insert Default Link';
}

header("location:$link");
exit;

?>

A word of warning, for some users in the UK their country code is returned as UK and others are GB so you might want an extra check for both codes.

I was already redirecting people through this link to help manage my links, this just adds an extra level to that redirect. It has already paid off making sure that users from the countries I specified were redirected to the correct website for them. I currently have this redirect installed on a shared server with the website added to Cloudflare.

If you need to know specific country codes Wikipedia has a list that you can use.

This is a pretty simple method and Cloudflare takes care of maintaining the IP Geolocation database for you. Of course you could do more sophisticated things than this but this gets the job done for me.

Htaccess Tools

If you want to save some time editing your .htaccess file check out Htaccess Tools. It has some great .htaccess generators to help save you some time. Generators include: Htpasswd Generator Htaccess Authentication Hotlink protection of images Block IPs with .htaccess Block hitbots with .htaccess Error Document Redirection by Language While you can do all… Continue Reading

LGR Photo Discontinued

Many years ago I wrote a quick PHP script to help me create a simple HTML photo gallery from JPG images. I released it as LGR Photo and gave it away for free. It was a great little gallery and did what I needed it to do. I made it mainly for my own purposes… Continue Reading

Basic HTML – ASCII HTML Codes

I am always amazed at how often I come back to a table of ASCII HTML codes that I have on my computer. ASCII HTML codes allow you to enter characters into a web page that are not found on you keyboard. The codes can also be very useful for entering characters for other languages,… Continue Reading

Basic HTML – Ordered and Unordered Lists

One of the easiest ways to add some organization to your blog posts is to use a list. People like lists and they can help you make your points in a post or on your website clearly and quickly. In HTML there are three types of lists: Ordered lists. Unordered lists. Definition lists. For this… Continue Reading

Visual HTML Jokes

I was surfing around a little last night and came across this visual HTML joke of the Leaning Tower of Pisa. I think you have to be a true HTML geek to think it is funny. That photo reminded me of a couple of photos of a truck that is embedded into the ground at… Continue Reading

Basic HTML – Links

One of the great things about publishing on the Internet is the ability to link to other resources on the Internet. A basic hyperlink in the body of a page is created using the anchor tag and looks like this: <a href="http://www.lgr.ca/">LGR Internet Solutions</a> The href (Hypertext Reference) attribute is the resource on the Internet… Continue Reading

Basic HTML – Headings

If you are new to running your own website or blog you might not know a lot of HTML but there is some basic HTML that is easy to learn that will make running and managing your website and blog much easier now and in the future. By using headings in your web pages and… Continue Reading