\n"; $ip = sprintf("%u", ip2long($addr)); // echo "Your IP, numerical: " . $ip . "

\n"; // Initiate the timer $time_start = microtime_float(); // Open the csv file for reading $handle = fopen("ip2country.csv", "r"); // Load array with start ips $row = 1; while (($buffer = fgets($handle, 4096)) !== FALSE) { $array[$row] = substr($buffer, 1, strpos($buffer, ",") - 1); $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); if ($ip >= $array[$row_midpt]) { $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 rewind($handle); $row = 1; while ($row <= $row_lower) { $buffer = fgets($handle, 4096); $row++; } $buffer = str_replace("\"", "", $buffer); $ipdata = explode(",", $buffer); // echo "Data retrieved from the csv file:
\n"; // echo "ipstart = " . $ipdata[0] . "
\n"; // echo "ipend = " . $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"; $iso2 = $ipdata[4] ; // 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); } /* ?>