Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 4th Nov 2010, 07:41 PM   #1
Warrior Member
War Room Member
 
Join Date: 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Redirect by country
Share on: 
fb share twitter share gplus share more share

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');
}
?>
...and Im using these files:

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.
sqallpl is offline  
Unread 4th Nov 2010, 10:56 PM   #2
VIP Warrior
War Room Member
 
ereadergold's Avatar
 
Join Date: 2008
Location: Montreal Canada
Posts: 2,390
Thanks: 1,017
Thanked 836 Times in 572 Posts
Blog Entries: 1
Default
Re: Redirect by country
Share on: 
fb share twitter share gplus share more share

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
ereadergold is offline  
Unread 4th Nov 2010, 11:13 PM   #3
Web Developer
 
Join Date: 2010
Location: Wisconsin
Posts: 322
Thanks: 2
Thanked 80 Times in 74 Posts
Default
Re: Redirect by country
Share on: 
fb share twitter share gplus share more share

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');

}

phpbbxpert is offline  
Unread 16th Nov 2010, 01:10 AM   #4
Warrior Member
War Room Member
 
Join Date: 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: Redirect by country
Share on: 
fb share twitter share gplus share more share

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
sqallpl is offline  
Unread 20th Nov 2010, 01:31 PM   #5
Warrior Member
War Room Member
 
Join Date: 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: Redirect by country
Share on: 
fb share twitter share gplus share more share

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);
bort27 is offline  
Unread 21st Nov 2010, 04:26 AM   #6
Warrior Member
 
purelogic's Avatar
 
Join Date: 2010
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: Redirect by country
Share on: 
fb share twitter share gplus share more share

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
purelogic is offline  
Closed Thread


Bookmarks

Tags
country, redirect


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 03:24 AM.