Short-fiction updated


I’ve made some behind the scenes changes to my website (the current jewel in the crown, with 1,500 visitors a day!).

I’ve always had some amazon links/adverts on the site, as being a free story site, it made sense to link to their books.  It’s never made much money, just some vouchers every other month or so.  I realised that I probably wasn’t doing as well as I could, due to the fact I only had links and a bookshop using the UK version of Amazon, and as more than 2/3rds of my visitors from America, I was probably losing out on their potential sales.

So I read around the net, and decided to ‘geo-code’ the visitors IP address, working out where in the world they are from using their IP.  It’s not 100% foolproof, but is probably better than looking at their browser country/language settings (as UK people may well be set to US).  I managed to download a free database of IP ranges to country codes (106,000 rows in CSV file), which I loaded into MySql.  I read up and actually did that encoding the IP ranges as spacial co-ordiates, building virtual ‘rectangles’ out of them.  Then using some custom/fancy SQL, I take the vistor IP, make it a ‘point’ in this virtual IP space, and find which range of IPs the point falls in.  The reason for doing that instead of a simple ‘between’ comparing strings?  Performance!  Using the spacial method is far faster, especially under load.

Once done and tested, I just user some simple PHP if statements, showing either USA Amazon widgets/adverts or UK, depending if their IP falls in Great Britain or not.

Author: Krispy

Webmaster of this and many websites over the years. I've been a Senior developer (Java JEE and Oracle DB, specialising in SEAM/JSF/Hibernate web development). Ex Lead Developer at New Scientist magazine, where I worked on creating a new WordPress based website, using almost 100% custom created widgets, plugins and theme. My own projects include websites created in WordPress and custom written with PHP and MySQL, and have my own dedicated linux server to run them from. Currently proud of; Mags Direct - a site I created for my work at the Frontline Group. An online magazine shop, which I created with almost zero budget within a week (since had a full theme upgrade). Found at Married, one cat. Beer fanatic (wife probably says bore) - real ales and craft beers. I have spent for too much on home brew gear, I create full mash craft beers. I spend a lot of time either going to gigs or organising them - I'm part of a small bunch of friends putting on DIY Punk shows called "The Scary Clown Presents..." our website can be found here; Also a fan of the local footy team - UP THE POSH!

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *