/    Sign up×
Community /Pin to ProfileBookmark

Country to IP problem.

Whenever I run this script I get wrong results for country.
all the database and files can be downloaded from [url]http://software77.net/cgi-bin/ip-country/geo-ip.pl[/url]
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 to should show Norway.

[code=php]
<?php

// Get the surfer’s ip address
$addr = getenv(“REMOTE_ADDR”);
echo “Your IP, IPv4: ” . $addr . “<br />n”;

$ip = sprintf(“%010u”, ip2long($addr));
echo “Your IP, numerical: ” . $ip . “<br /><br />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<br /><br />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<br /><br />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:<br />n”;
echo “ipstart = ” . sprintf(“%010u”, $ipdata[0]) . “<br />n”;
echo “ipend = ” . sprintf(“%010u”, $ipdata[1]) . “<br />n”;
echo “registry = ” . $ipdata[2] . “<br />n”;
echo “assigned = ” . date(‘j.n.Y’, $ipdata[3]) . “<br />n”;
echo “iso2 = ” . $ipdata[4] . “<br />n”;
echo “iso3 = ” . $ipdata[5] . “<br />n”;
echo “country = ” . $ipdata[6] . “<br /><br />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 secondsn”;

function microtime_float()
{
list($usec, $sec) = explode(” “, microtime());
return ((float)$usec + (float)$sec);
}

?>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@saudauthorJul 19.2008 — Is there anyone who can find out the problem ?
Copy linkTweet thisAlerts:
@rootJul 19.2008 — Read this thread... http://www.webdeveloper.com/forum/showthread.php?t=186402

It may answer your query.
Copy linkTweet thisAlerts:
@felgallJul 19.2008 — That makes it sound like you can get about 94% accuracy on which country someone is in from their IP address provided that you update your IP list at least daily.

Presumably you could update it weekly and still have about 90% accuracy.

The most important thing to remember is that an IP address identifies the location of the company, ISP, or proxy server that it has been allocated to and not the location of the computer currently using it (which can be anywhere in the world but will most likely be in the same country).
Copy linkTweet thisAlerts:
@saudauthorJul 19.2008 — 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-country/geo-ip.pl?action=download

Main website: http://software77.net/cgi-bin/ip-country/geo-ip.pl
×

Success!

Help @saud spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...