cvs: phpweb /include ip-to-country.inc

From: Date: Sun, 25 May 2003 12:39:27 +0000
Subject: cvs: phpweb /include ip-to-country.inc
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-18073@lists.php.net to get a copy of this message
goba Sun May 25 08:39:27 2003 EDT Modified files: /phpweb/include ip-to-country.inc Log: Drop out ugly workaround to make the country lookup work The database has errors, so we need to wait for that to be fixed... Some speed fixes: - do not search in the DB if we were unable to find any helpful data in the index. A full DB search is something we should not do because of performance reasons - return a two element array in the index lookup specifying a range where the IP should be in the DB (if it is correct). If we pass that range, we were unable to find the IP. This reduces the number of compared records significantly Note that this is not a fix for Rasmus' and others' lookup problems. That is caued by the DB containing overlapping IP ranges, which are not OK for our indexer, and would even not return valid results in an SQL 'BETWEEN' search [which is the recommended search method on ip-to-country.com]. Index: phpweb/include/ip-to-country.inc diff -u phpweb/include/ip-to-country.inc:1.15 phpweb/include/ip-to-country.inc:1.16 --- phpweb/include/ip-to-country.inc:1.15 Sat May 24 16:51:41 2003 +++ phpweb/include/ip-to-country.inc Sun May 25 08:39:26 2003 @@ -1,6 +1,6 @@ <?php // -*- C++ -*- -// $Id: ip-to-country.inc,v 1.15 2003/05/24 20:51:41 jacques Exp $ +// $Id: ip-to-country.inc,v 1.16 2003/05/25 12:39:26 goba Exp $ /* @@ -60,25 +60,23 @@ // Convert the IP number to some useable form for searching $ipn = (float) sprintf("%u", ip2long($ipnum)); - + // Find the index to start search from $idx = i2c_search_in_index($ipn); - // For some odd reason I need to remove 2 from the idx value to - // get the country of rasmus and various other IP's to lookup. - $country = "NA"; - $count = 1; - - // Search in the database from the given index and if not found - // we attempt 3 lookups in total moving the index down one. - while ($country == "NA" && $count < 4) { - $country = i2c_search_in_db($ipnum, $idx); - $idx--; - $count++; + + // If we were unable to find any helpful entry + // in the index do not search, as it would take + // a very long time. It is an error, if we have + // not found anything in the index + if ($idx !== FALSE) { + $country = i2c_search_in_db($ipn, $idx); + } else { + $country = 'NA'; } // Set global variable for further usage $COUNTRY = $country; - + // Set the country in a cookie for a week return mirror_setcookie("COUNTRY", "$country,$ipnum", 60*60*24*7); @@ -97,7 +95,7 @@ $_SERVER['DOCUMENT_ROOT'] . "/backend/ip-to-country.idx", "r" ); - if (!$dbidx) { return 0; } + if (!$dbidx) { return FALSE; } // Read in granularity from index file and // convert current IP to something useful @@ -112,7 +110,7 @@ // Compare current index part with our IP if ($ip_chunk >= $idxpart && $ip_chunk <= (int) $data[0]) { - return $recnum; + return array($recnum, (int) $data[1]); } // Store for next compare @@ -121,12 +119,12 @@ } // Return record number found - return $recnum; + return array($recnum, -1); } // Find the country searching from record $idx // $ip should be an IP number and not an IP address -function i2c_search_in_db($ip, $idx = 0) +function i2c_search_in_db($ip, $idx) { global $_SERVER; @@ -144,15 +142,23 @@ if (!$ipdb) { return $country; } // Jump to record $idx - fseek($ipdb, $idx*24); - + fseek($ipdb, $idx[0]*24); + // Read records until we hit the end of the file, - // or we find the range where this IP is + // or we find the range where this IP is, or we + // reach the next indexed part [where the IP should + // not be found, so there is no point in searching further] while (!feof($ipdb) && !($range_start <= $ip && $range_end >= $ip)) { + // We had run out of the indexed region, + // where we expected to find the IP + if ($idx[1] != -1 && $idx[0] > $idx[1]) { + $country = "NA"; break; + } + // Try to read record $record = fread($ipdb, 24); - + // Unable to read the record => error if (strlen($record) != 24) { $country = "NA"; break; } @@ -160,6 +166,9 @@ $range_start = (float) substr($record, 0, 10); $range_end = (float) substr($record, 10, 10); $country = substr($record, 20, 3); + + // Getting closer to the end of the indexed region + $idx[0] += 1; } // Close datafile

« previous php.mirrors (#18073) next »