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

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

?>

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

Recommended Answers

All 50 Replies

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

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

header('Location: german_page.php');
exit;

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

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

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";
?>

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.

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

Member Avatar for diafol
<?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)
[B]echo[/B] '<p>Code = ' . geoip_country_code_by_addr($GeoIPDatabase, $IP) . '</p>';


//to get the full country name
[B]echo[/B] '<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?

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.

Member Avatar for diafol

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.

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 ??

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

commented: Thanks Xan, what a heel +3

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

Member Avatar for diafol

So what are you having problems with now? Did you try the bits of code offered previously? Do they work?

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

Member Avatar for diafol

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

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

Member Avatar for diafol

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 
}

Hello.
I place the code and not working. You by Mozilla to view the code on this site http://answer23.100free.com/ and tell me where wrong
Thanks a lot

Member Avatar for diafol

OK I see your problem. You haven't placed your php code within php tags:

[B]<?php[/B]
$code = strtolower(geoip_country_code_by_addr($GeoIPDatabase, $IP));
 
switch($code){
   case 'ca':
      header("Location: http://answer23.100free.com/index1.html"); //strictly speaking this should be the full url
      break;
   case 'de':
     header("Location: http://answer23.100free.com/index1.html");
     break;
   case 'mk':
     header("Location: http://answer23.100free.com/index.php");
     break;
}
[B]?>[/B]

You'll also need to include the "include files" within the page before the above code.

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

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

Hi

The code that you post works me here but I notice that only the country code (2 letters) appear on my site so how to get the full country name? And also If you have a code that will identify visitors city name of a his country please post

Thanks..

Member Avatar for diafol
//to get the full country name
geoip_country_name_by_addr($GeoIPDatabase, $IP);

This was posted earlier

Thank you so much.. it really helps and it works here..

What about the identify the visitors city?

Member Avatar for diafol

I think the IP address gives the IP of the ISP, not the user. But I'm not sure about this. However, I've seen some pop-up adverts with my area on them - never understood how they did this.

Member Avatar for diafol

I'm not going to wade through the last 2 files. Just show us your main page (all of it). PHP code cannot be viewed from a browser if the page has a .php extension.

Member Avatar for diafol

Here's an implementation of the sample code for PHP:

include("{$_SERVER['DOCUMENT_ROOT']}/includes/geoip.inc");
$gi = geoip_open("{$_SERVER['DOCUMENT_ROOT']}/includes/GeoIP.dat",GEOIP_STANDARD);

echo geoip_country_code_by_addr($gi, "24.24.24.24") . "\t" .
     geoip_country_name_by_addr($gi, "24.24.24.24") . "\n";
echo geoip_country_code_by_addr($gi, "80.24.24.24") . "\t" .
     geoip_country_name_by_addr($gi, "80.24.24.24") . "\n";

geoip_close($gi);

Create an "includes" folder in your root and place the 2 files there (geoip.inc and GeoIP.dat).

You need to download the DB (GeoIP.dat).

If you've downloaded the C library - it needs to be installed with Apache. If you're running on a hosted shared server, forget this, just go for the Pure PHP version above.

I'm going to play around with the download to see if it works. I'll get back to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.