saud Posted August 9, 2008 Share Posted August 9, 2008 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 . "<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 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 Quote Link to comment https://forums.phpfreaks.com/topic/118962-help-with-country-ip-script/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.