blackxxxer
BANNED
- Oct 29, 2008
- 136
- 57
Does anyone know of or have a script that geo-targets url's by country? I need something that will detect the ip/country & send the user to a URL based off country?
Thanks
Thanks
<?php
// Get country code corresponding to an IP address
function get_country_code ($ip) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://www.topwebhosts.org/whois/index.php?query=$ip");//otwieramy łacze ze strona
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER,1);
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686;pl; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3');
curl_setopt($c, CURLOPT_ENCODING, 'gzip');
curl_setopt($c, CURLOPT_ENCODING, 'deflate');
curl_setopt($c, CURLOPT_ENCODING, '');
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
$page = curl_exec($c);
$code = explode('country',$page);
$code = explode(':',$code[1]);
$code = explode('admin-c:',$code[1]);
$country_code=trim($code[0],"admin-c:");
return $country_code;
}
// Detect user's IP address
$user_ip = $_SERVER["REMOTE_ADDR"];
// COUNTRY CODE, eg. us, fr, vn
$country_code = get_country_code($user_ip);
switch($country_code)
{
case 'US':
//redirect to US page
break;
case 'UK':
//redirect to FR page
break;
case 'CAN':
//redirect to CAN page
break;
case 'AUS':
//redirect to AUS page
break;
//etc...etc...etc here you can ad more countries
default://other coutries
//default redirect
}
?>
Code:http://nytemarez.com/geofiltering-the-right-way/
I'd prefer to not use a mysql db for geofiltering since it would mean importing data every month into a db AND would make it a much bigger pain in the ass to move to other servers. Using the method I posted with a binary db file you could just copy the files to another server for a working solution.
With binary files (even with PEAR) you are making access incredibly slow compared to a well-indexed database table. Also I don't find it very hard to setup a cronjob which basically imports the new database everyday. If you setup a UNIQUE-constraint on start_ip and end_ip you will have no problems importing it in less than 30 secs. Don't know about you, but for my solution I needed very quick data access, especially since I'm getting alot of hits everyday.