| | #1 |
| Warrior Member War Room Member Join Date: 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
I need to redirect my traffic by visitors country. I have one method for doing this but the code is not what i exactly need. Im using this code: Code: <?php
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
if (!in_array(strtolower($country), $my_countries))
{
header('Location: http://www."ALL"TRAFFICURLGOESHERE.whatever');
}
else
{
header('Location: http://www."SELECTEDCOUNTRIES"URLGOESHERE.whatever');
}
?> http://geolite.maxmind.com/download/...y/GeoIP.dat.gz http://geolite.maxmind.com/download/.../php/geoip.inc It works but only for one group or one country. This script is sending the visitors from selected countries to second url and other IP's to first url. I need a code that will send different countires to different URL's. I need to set up few countries (one url per country) and one url for other countries, that are not metioned in the code. For example: USA -> url1 GB -> url2 CA -> url3 All other countries -> url4 Can anyone help me? I think its simple for advenced coder, but I have 0 experience in PHP. I think that its possible to use these two files with data that Ive metioned. Help will be much appreciated. |
| |
| | #2 |
| VIP Warrior War Room Member Join Date: 2008 Location: Montreal Canada
Posts: 2,390
Thanks: 1,017
Thanked 836 Times in 572 Posts
Blog Entries: 1 |
Just change your "if" statement: if ($country=="us") { header('Location: http://www.USA.whatever'); } elseif ($country=="gb") { header('Location: http://www.GREATBRITAN.whatever'); } elseif ($country=="ca") { header('Location: http://www.CANADA.whatever'); } else { header('Location: http://www."OTHERCOUNTRIES.whatever'); } Bill |
| |
| | #3 |
| Web Developer Join Date: 2010 Location: Wisconsin
Posts: 322
Thanks: 2
Thanked 80 Times in 74 Posts
|
If you are going to have a long logistic condition it may be better to use a switch switch ($country) { case 'gb': header('Location: http://www.GREATBRITAN.whatever'); break; case 'ca': header('Location: http://www.Canada.whatever'); break; case 'fr': header('Location: http://www.France.whatever'); break; default: header('Location: http://www.some_default.com'); } |
| | |
| |
| | #4 |
| Warrior Member War Room Member Join Date: 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
It worked for me but only with this code: http://shockmarketer.com/country-red...direct.php.zip You can select the countires at the end. You want to use a recent ip database: http://geolite.maxmind.com/download/...y/GeoIP.dat.gz |
| |
| | #5 |
| Warrior Member War Room Member Join Date: 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
If you have several country/URL pairs, you'll find it easier to maintain if you do something like the following: require_once 'geoip.inc'; $urls_countries = array( 'US' => 'htp://USA_URL/', 'CA' => 'htp://CANADA_URL/', 'GB' => 'htp://BRITAIN_URL/', 'FR' => 'htp://FRANCE_URL/', ); $gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE); $url = $urls_countries[geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR'])]; $url = $url ? $url : 'htp://DEFAULT_URL/'; header("Location: $url", TRUE, 301); |
| |
| | #6 |
| Warrior Member Join Date: 2010
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
|
You should just change the if statement of your like this if ($country=="us") { header('Location:usa.php'); } for all your countries and it will work fine |
| |
|
| Bookmarks |
| Tags |
| country, redirect |
| |