Using php to check location of user for ad switching?

I’ve got a little php script setup to randomly switch between three ads. One of these ads is global but the other two are for amazon.com and amazon.co.uk and I was thinking it might be better to ensure that amazon.co.uk only gets shown to visitors from the UK. Is this a doable check in php? What I have right now is this:


<?php

$ad[] =
'AD 1 - Global'
;

$ad[] =
'AD 2 - Amazon.com - Should be Global except UK'
;

$ad[] =
'AD 3 - Amazon.co.uk - Should be UK only'
;

srand ((double) microtime() * 9999999);
$random_number = rand(0,count($ad)-1);

echo ($ad[$random_number]);

?>

I’d imagine the first step would be to randomize the selection between AD 1 and AD 2 + AD 3. And if it hits AD 2 + AD 3, make a selection based on location.

Here’s a start for ya :slight_smile:

http://php.net/manual/en/book.geoip.php


if (geoip_continent_code_by_name($userIP) == "GB") {
// randomly select from UK ads
} else {
// randomly select from non UK ads
}

Well, I did not know that was in pecl, had always installed maxminds’ ext myself. Thanks.

Thank you, I will see where I can get with that. Handy function. :slight_smile: