954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Block IP country

Hi I found this code and do as it should but I just code as to add my website

Well first you need to download the GeoLite Country database in binary format http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz . Next, upload it to your server. Afterward, download the http://geolite.maxmind.com/download/geoip/api/php/geoip.inc file and save it on your server as well.

Finally, in your application, use the code below (you can modify it): [PHP]<?php

include("/home/new/public_html/geoip.inc");

//open the database $GeoIPDatabase = geoip_open("/home/new/public_html/GeoIP.dat", GEOIP_STANDARD);

//to get the country code (2 letters) geoip_country_code_by_addr($GeoIPDatabase, $IP);

//to get the full country name geoip_country_name_by_addr($GeoIPDatabase, $IP);

//close the database geoip_close($GeoIPDatabase);

?>[/PHP]

Code works but now I need to incorporate code to do what I need
For example, if you're from germany you open the page 2, but if you're from Italy to open a page 3.
Something if and else but I do not know how to do it.

Thank you very much for the help

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 
$code = geoip_country_code_by_addr($GeoIPDatabase, $IP);
if ($code == "de") {
  // goto page for germany
}
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

to get it to go to the german page then you'd use

header('Location: german_page.php');
exit;
phper
Posting Whiz in Training
213 posts since Nov 2006
Reputation Points: 22
Solved Threads: 19
 

This code has set the page to Germany and did not work

$code = geoip_country_code_by_addr($GeoIPDatabase, $IP);
if ($code == "de") {
  // goto page for germany
}

This code place page, and Germany does not work phper
1.
header('Location: german_page.php');
2.
exit;

How wrong I do not understand is needle like.

Thank you

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 

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
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Setting an example from the site and am interested in it, where should I set the code that distinguishes it when someone from Italy can not enter this site and to transfer to another page that is Italy. Why something can not succeed with me, or I have something wrong.
Thank you

<?php
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"; 
echo "<html>\n"; 
echo "<head>\n"; 
echo "<title>Untitled</title>\n"; 
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\n"; 
echo "<meta name=\"generator\" content=\"german\">\n"; 
echo "\n"; 
echo "<style type=\"text/css\">\n"; 
echo "/*----------Text Styles----------*/\n"; 
echo ".ws6 {font-size: 8px;}\n"; 
echo ".ws7 {font-size: 9.3px;}\n"; 
echo ".ws8 {font-size: 11px;}\n"; 
echo ".ws9 {font-size: 12px;}\n"; 
echo ".ws10 {font-size: 13px;}\n"; 
echo ".ws11 {font-size: 15px;}\n"; 
echo ".ws12 {font-size: 16px;}\n"; 
echo ".ws14 {font-size: 19px;}\n"; 
echo ".ws16 {font-size: 21px;}\n"; 
echo ".ws18 {font-size: 24px;}\n"; 
echo ".ws20 {font-size: 27px;}\n"; 
echo ".ws22 {font-size: 29px;}\n"; 
echo ".ws24 {font-size: 32px;}\n"; 
echo ".ws26 {font-size: 35px;}\n"; 
echo ".ws28 {font-size: 37px;}\n"; 
echo ".ws36 {font-size: 48px;}\n"; 
echo ".ws48 {font-size: 64px;}\n"; 
echo ".ws72 {font-size: 96px;}\n"; 
echo ".wpmd {font-size: 13px;font-family: 'Arial';font-style: normal;font-weight: normal;}\n"; 
echo "/*----------Para Styles----------*/\n"; 
echo "DIV,UL,OL /* Left */\n"; 
echo "{\n"; 
echo " margin-top: 0px;\n"; 
echo " margin-bottom: 0px;\n"; 
echo "}\n"; 
echo "</style>\n"; 
echo "\n"; 
echo "<style type=\"text/css\">\n"; 
echo "div#container\n"; 
echo "{\n"; 
echo "	position:relative;\n"; 
echo "	width: 300px;\n"; 
echo "	margin-top: 0px;\n"; 
echo "	margin-left: auto;\n"; 
echo "	margin-right: auto;\n"; 
echo "	text-align:left; \n"; 
echo "}\n"; 
echo "body {text-align:center;margin:0}\n"; 
echo "</style>\n"; 
echo "\n"; 
echo "</head>\n"; 
echo "\n"; 
echo "<body bgColor=\"#000000\">\n"; 
echo "\n"; 
echo "<div id=\"container\">\n"; 
echo "<div id=\"image1\" style=\"position:absolute; overflow:hidden; left:0px; top:195px; width:300px; height:300px; z-index:0\"><img src=\"images/puma.bmp\" alt=\"\" title=\"\" border=0 width=300 height=300></div>\n"; 
echo "\n"; 
echo "\n"; 
echo "</div></body>\n"; 
echo "</html>\n"; 
echo "\n";
?>
perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 

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
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hello.
I place the code as I wrote in place of ip entered my site but I still opens the same page. See the code I took one example of this link
http://answer23.100free.com/
I think it lacks direction to the other site.

file in my ftp:

GeoIP.dat.gz
geoip.inc
index.php


Thank you

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 
<?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)
Moderator
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.

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 
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)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

so how do you unblock the IP adress if lets say, eurosport is streaming Soccor and i want to see it but when you click livefeed, it saids it is unavailable in your cournty cause your IP adress ??

Janiceps
Light Poster
49 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

@Jan:
This is going off-topic, but you need to get a proxy - see here:
http://proxy.org/forum/1175967005.html

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Again, off topic, but I think he is trying to get help for this still :P

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

I think my question was very clearly written that I do ask you to help me and write if possible. the proxy can not enter but I'm not interested and do not need to be commented

Thanks a lot

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 

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)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

to me does not work because the post and asked someone to try and say. So if you try and write if you work with.

Thanks a lot

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 

Try google translate? I'd like to help but, I can't understand you.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

1.OK. As I noticed you have not read my first post I have written from there to begin. I set some things I have found but do not know how my index.php page instance to connect to all those things.

2.And all that I need to be able to automatically control the site depending on if the visitors are not from Italy will open another page while those from Italy will enter the site properly.
I hope I understood what I do

3.Can the second question to submit your idea how it would an easy way to make


Thanks a lot

perosf
Newbie Poster
24 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
 

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)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You