Gossamer Forum
Home : General : Perl Programming :

convert IP to Country

Quote Reply
convert IP to Country
Hi

I would like to convert IPs (remote address) to a country.

Suggestions?

Last edited by:

robyone: Nov 13, 2002, 5:57 AM
Quote Reply
Re: [robyone] convert IP to Country In reply to
http://search.cpan.org/...P-0.26/lib/Geo/IP.pm
Quote Reply
Re: [robyone] convert IP to Country In reply to
If you've got a MySQL database you could try http://www.ip2nation.com
Quote Reply
Re: [Jome] convert IP to Country In reply to
How does this work exactly?

I only need to resolve dutch or german visitors...Could I use this techique to write a small perl snippet to do the same? Would be great!
Quote Reply
Re: [cK] convert IP to Country In reply to
In Reply To:
How does this work exactly?

I only need to resolve dutch or german visitors...Could I use this techique to write a small perl snippet to do the same? Would be great!


It's a MySQL table so you would need access to a MySQL database, or any SQL database should work really (with some modification of the insert code). If you use Perl there's DBI/MySQL.pm or something like that which make it possible to communicate with a MySQL DB.

In the attached file (coming with the SQL-file when you download) there's a short query which is to be used for seeing if a visitor is german or dutch for instance.

Do you have DB access? (of any kind)
Quote Reply
Re: [Jome] convert IP to Country In reply to
I just requested MYSQL at my hoster, so database access is coming up...

(I asked the question above because I first wanted to extract only the data for europe due to the fact that I didn't have access to a sql-database. But now I have access, I can just install it all...)

My only problem is: I NEVER WORKED WITH SQL before... (I'm pre-sql ;-)

Therefore I would like to know where I can get good information to add this single line into a short perl I use to determine the referer...
Code:
SELECT country FROM ip2nation WHERE ip < INET_ATON('<ip>') ORDER BY ip DESC LIMIT 0,1;



I now use: (the script runs on first pagina (index.html) so all other pages can access this info later on.)

Code:

my $in = new CGI;
my $string = $in->param('ID');

my $referer = $ENV{'HTTP_REFERER'};
$referer =~ s|^http://([^/]+).*|$1|i;

my $country = $sqllookup
my $cookie = $country . "-" . $referer

my ($crypt, $random, $id);
$crypt = join ("-", map { ord } (split //, $cookie));
($string =~/-/) ? ($random,$id) = split(/-/,$string) : $id = $string;

my $cookie = $in->cookie(
-name => 'id',
-value => $crypt,
-domain => '.domain.com',
-expires => '+12h',
-path => '/'
);

Last edited by:

cK: Apr 25, 2003, 2:31 AM
Quote Reply
Re: [cK] convert IP to Country In reply to
This is what I got working in PHP.. Comments are very welcome! (This is my first php/sql script)

Now I need to get this working in perl...

Code:

<?php

$server="localhost";
$user="xxx";
$pass="xxx";
$db="xxx";

$ip = $_SERVER['REMOTE_ADDR'];

mysql_connect($server, $user, $pass) or die("Unable to connect to SQL server.");
mysql_select_db($db) or die ("Unable to connect to SQL server.");

$sql="SELECT country FROM ip2nation WHERE ip < INET_ATON('$ip') ORDER BY ip DESC LIMIT 0,1";
$result = mysql_query($sql) or die ("Invalid query: " . mysql_error());

while ($row = mysql_fetch_object($result)) {
$countrycode = $row->country;
}

echo "Your IP Address is $ip<br>You're located in '$countrycode'";
mysql_free_result($result);

?>

Last edited by:

cK: Apr 25, 2003, 8:22 AM