PDA

View Full Version : GeoTargetting Ads



Generalissimo
03-11-2007, 03:51 PM
I've bought a copy of MaxMind's db,

and now I'm trying to create a page that I will link an advertisement to that will then go to one of three urls based on what country the user is from (UK, USA or any other country)...

The code does not seem to work as it directs me to the international URL when I'm from the UK... Does anyone have any idea what I've done wrong?


<html>
<head>
<meta http-equiv="refresh" content="0;url=<?
$DatabaseServer = "localhost";
$Username = "user";
$Password = "pass";
$DatabaseName = "db";

$link = mysql_connect($DatabaseServer, $Username, $Password) or die('Could not connect: ' . mysql_error());
mysql_select_db($DatabaseName) or die('Could not select database');
$IP = $_SERVER["REMOTE_ADDR"]; //Get the IP address
$res = mysql_query("SELECT country_code2,country_name FROM IPCountries WHERE IP_FROM<=inet_aton('$IP') AND IP_TO>=inet_aton('$IP')");//look up IP address


if($CountryCode == "GB"){
//echo url for United Kingdom
echo "UKURL";
}
if($CountryCode == "US"){
//echo url for USA
echo "USURL";
}
else{
//default international url
echo "INTURL";
}


mysql_close($link); //clean up
?>">;
</head></html>

Todd W
03-11-2007, 03:53 PM
Maybe your IP is getting confused?

How come you are not using the apache & c module w/.htaccess less overhead.

-Todd

rpanella
03-11-2007, 08:39 PM
After you are doing the query you are not doing anything with the result so $CountryCode is never set, so it will always show the international url. Adding this after the line where you do the query should fix it.


list($CountryCode,$CountryName) = mysql_fetch_row($res);

It would be faster and simpler though to use the PHP module they provide since you won't need to use mysql at all: http://www.maxmind.com/app/php
________
FORD ESCAPE HYBRID SPECIFICATIONS (http://www.ford-wiki.com/wiki/Ford_Escape_Hybrid)

Generalissimo
03-12-2007, 12:48 AM
Thanks so far. Does anyone have a tutorial about how to use the PHP module they provide to do .htaccess?

rpanella
03-12-2007, 02:18 AM
There are two different things, there is a PHP module, which is as easy as uploading their binary database file and a php file that you include in your script, and then just call a function to get the country. That is what I linked to above. I believe this site has an article about implementing that written by Westech.

They also have an Apache module which you can install and then redirect using htaccess. It is more complicated but is slightly faster if you are going to be doing a lot of geotargeting on every pageview.
________
FZ1 (http://www.cyclechaos.com/wiki/Yamaha_FZ1)

Westech
03-12-2007, 07:16 AM
Here's a link to the article rpanella mentioned: http://www.websitepublisher.net/article/geotargetting-php/

Also, in the code you listed you're using "if...if...else". To do what you want I think you'll need to instead use "if...elseif...else".

Generalissimo
03-12-2007, 09:28 AM
Your example of smiley ads was exactly what I needed this for - thanks John!

Generalissimo
03-12-2007, 10:58 AM
John, it will only work once per page - what do I do? (Sorry, I'm sure this is probably basic PHP about closing a connection, but I don't even know the basics)

Westech
03-12-2007, 12:05 PM
No problem.

This part of the code sets the $cc variable to hold the visitor's country code:



include("geoip.inc"); // include the geoip functions
$geofile = geoip_open("GeoIP.dat",GEOIP_STANDARD); // open the geoip data file
$cc = geoip_country_code_by_addr($geofile, $_SERVER['REMOTE_ADDR']);
geoip_close($geofile); // close the data file


It should only appear once per page. If you want to include another geotargetted ad later in the same page leave out the code above (so it only appears once per page) and just include the if... part again. This will work since $cc is already set and doesn't need to be set again.