Forum Topics : Do you use the IP-to-Country Database? / monster-submit.com
Just imported the IP-to-Country database into newly created mySQL database, and wrote a CGI-Perl function (incorporated into billing script) to screen out purchases from certain countries. Good work on the database! If anyone is interested, here is the (modified - e.g., error and result handling omitted or modified) code snippet:
print "Content-type: text/html\n\n";
my @numbers = split(/\./, $ENV{'REMOTE_ADDR'});
my $code = ($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
use DBI();
my $dbhost = "localhost";
my $dbname = "geoip";
my $dbuser = "XXXXXXXXX";
my $dbpass = "XXXXXXXXX";
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpass");
my $country_query = "SELECT country_code2,country_name FROM iptocountry WHERE ip_from<=$code AND ip_to>=$code";
my $sth = $dbh->prepare(qq{$country_query});
$sth->execute();
my ($country_code, $country_name);
while(my $ref = $sth->fetchrow_hashref()) {
$country_code = $ref->{'country_code2'};
$country_name = $ref->{'country_name'};
}
$sth->finish;
$dbh->disconnect;
print "$country_code - $country_name";
