Whenever I run this script I get wrong results for country.
all the database and files can be downloaded from http://software77.net/cgi-bin/ip-country/geo-ip.pl
But the Perl script gives the correct result. All the files are listed there.
If i change the $addr to "213.6.49.205" it shows wrong results and should show Norway.
<?php
// Get the surfer's ip address
$addr = getenv("REMOTE_ADDR");
echo "Your IP, IPv4: " . $addr . "
\n";
$ip = sprintf("%010u", ip2long($addr));
echo "Your IP, numerical: " . $ip . "
\n";
// Initiate the timer
$time_start = microtime_float();
// Open the csv file for reading
$handle = fopen("IpToCountry.csv", "r");
// Load array with start ips
$row = 1;
while (($buffer = fgets($handle, 9096)) !== FALSE) {
$array[$row] = $buffer;
$row++;
}
// Time loading
$time_end = microtime_float();
$time = substr($time_end - $time_start, 0, 7);
echo "Array with " . $row . " start ips loaded in $time seconds
\n";
// Locate the row with our ip using bisection
$row_lower = '0';
$row_upper = $row;
while (($row_upper - $row_lower) > 1) {
$row_midpt = (int) (($row_upper + $row_lower) / 2);
$buffer = $array[$row_midpt];
$start_ip = sprintf("%010u", substr($buffer, 1, strpos($buffer, ",") - 1));
if ($ip >= $start_ip) {
$row_lower = $row_midpt;
} else {
$row_upper = $row_midpt;
}
}
// Time locating
$time_end = microtime_float();
$time = substr($time_end - $time_start, 0, 7);
echo "Row with your ip (# " . $row_lower . ") located after $time seconds
\n";
// Read the row with our ip
$buffer = $array[$row_lower];
$buffer = str_replace("\"", "", $buffer);
$ipdata = explode(",", $buffer);
echo "Data retrieved from the csv file:
\n";
echo "ipstart = " . sprintf("%010u", $ipdata[0]) . "
\n";
echo "ipend = " . sprintf("%010u", $ipdata[1]) . "
\n";
echo "registry = " . $ipdata[2] . "
\n";
echo "assigned = " . date('j.n.Y', $ipdata[3]) . "
\n";
echo "iso2 = " . $ipdata[4] . "
\n";
echo "iso3 = " . $ipdata[5] . "
\n";
echo "country = " . $ipdata[6] . "
\n";
// Close the csv file
fclose($handle);
// Total execution time
$time_end = microtime_float();
$time = substr($time_end - $time_start, 0, 7);
echo "Executing the whole script took $time seconds\n";
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
?>
The thing is that IP is correctly mentioned in the database, just the PHP script is not reading the database correctly, while the CGI script is doing it correctly. DO a test yourself by downloading both the files, they are free too.
http://webnet77.com/scripts/geo-ip/IP-country-FREE.zip
Database download: http://software77.net/cgi-bin/ip-cou...ction=download
Main website: http://software77.net/cgi-bin/ip-country/geo-ip.pl