$code = geoip_country_code_by_addr($GeoIPDatabase, $IP);
if ($code == "de") {
// goto page for germany
}
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
you forgot to set $IP. The following works for me:
<?php
include 'geoip.inc';
$IP = '94.214.104.191';
//open the database
$GeoIPDatabase = geoip_open('GeoIP.dat', GEOIP_STANDARD);
//to get the country code (2 letters)
$country_code = geoip_country_code_by_addr($GeoIPDatabase, $IP);
switch ($country_code) {
'NL': break; // codes are in uppercase !
}
//to get the full country name
$country_name = geoip_country_name_by_addr($GeoIPDatabase, $IP);
//close the database
geoip_close($GeoIPDatabase);
?>
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
You should set the code from my example at the top of your page. And use the referer's ip address to check the country.
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
<?php
include("/home/new/public_html/geoip.inc");
$IP = $_SERVER['REMOTE_ADDR'];
//open the database
$GeoIPDatabase = geoip_open("/home/new/public_html/GeoIP.dat", GEOIP_STANDARD);
//to get the country code (2 letters)
<strong>echo</strong> '<p>Code = ' . geoip_country_code_by_addr($GeoIPDatabase, $IP) . '</p>';
//to get the full country name
<strong>echo</strong> '<p>Name = ' . geoip_country_name_by_addr($GeoIPDatabase, $IP) . '</p>';
//close the database
geoip_close($GeoIPDatabase);
?>
If you use the above, you should see the two lines, for example:Code = de
Name = Germany
I don't use this, so the code may be uppercase and the name may be in the native language (I'm guessing here).
In order to redirect properly - force the code to be lowercase:
$code = strtolower(geoip_country_code_by_addr($GeoIPDatabase, $IP));
switch($code){
case 'de':
header("Location: de.php"); //strictly speaking this should be the full url
break;
case 'it':
header("Location: it.php");
break;
default:
header("Location: en.php");
break;
}
This redirects the user to the country language or their main country language doesn't exist, they get sent to the English pages.
Personally, I have a problem with automatic redirects as they may send an user to a wrong language. If an English speaker is accessing your site from a hotel PC while on holiday in Rome, they'll get redirected to the Italian page automatically, etc etc.
Would not a language dropdown be more useful?
BTW: avoid flags at all costs! Some countries have more than one official language and some languages are common to more than one country - this may force a German-speaking Swiss or Austrian to choose the German flag, which may cause irritation!
//EDIT
Sorry, just noticed the title of the post - you want to block access from a particular country? Any good reason for this?
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
So map will be paying so I want to different parts of the two groups example Italy, where the price will be a while for other countries will be different because I want to IP-distinguishes it to you directly to the page that opens necessary.
Ahh. I see. Sorry, I thought you were just trying to implement a convenient way for users to go to a language-specific page. My mistake. Well, in that case try the code posted previously. Instead of the actual redirects you could echo the $code and supply a manual $IP to check if it works before implementing.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
So what are you having problems with now? Did you try the bits of code offered previously? Do they work?
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Try google translate? I'd like to help but, I can't understand you.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
With respect, I did read your first post and have offered some assistance, as below:
$code = strtolower(geoip_country_code_by_addr($GeoIPDatabase, $IP));
switch($code){
case 'de':
header("Location: de.php"); //strictly speaking this should be the full url
break;
case 'it':
header("Location: it.php");
break;
default:
header("Location: en.php");
break;
}
You didn't reply or comment on the code. Which part does not work?
If you want Italy-based users to go to the site but others to go elsewhere:
$code = strtolower(geoip_country_code_by_addr($GeoIPDatabase, $IP));
if($code != 'it'){
header("Location: whateverpage.php"); //strictly speaking this should be the full url
}
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080