Forum Moderators: coopster
3642228736,3642232831,DE
3642232832,3642236927,RS
3642236928,3642241023,CH
3642241024,3642245119,DE
3642245120,3642249215,LV
3642249216,3642253311,FR
3642253312,3642257407,FI
3642257408,3642261503,RU
3642261504,3642265599,GB
3642265600,3642265855,IR
function ipcountrycode($ip){
$ip=sprintf("%u",ip2long($ip));
$low=0;
$csvfilename="include/ipCC.csv";
$fp=fopen($csvfilename,"r");
fseek($fp,0,SEEK_END);
$high=ftell($fp);
$CC='US';
while($low<=$high){
// Move to mid point between low and high
$mid=floor(($low+$high)/2);
fseek($fp, $mid);
// Moves to end of line
$line=fgets($fp); // Is this actually doing anything of value?
// Turn line into an array.
$ipdata=fgetcsv($fp,26);
// if IP bigger than column 2 then seek higher.
if($ip>$ipdata[1]){$low=$mid+1;}
// if IP smaller than column 1 then seek lower.
elseif($ip<$ipdata[0]){$high=$mid-1;}
// We've found a match.
else{
$CC=$ipdata[2];
$low=999999999;
}
}
fclose($fp);
unset($line);
return $CC;
}
Now if you were to store the 2 character code for every single IP address on a new line,
you would end up with a file roughly 12 GB in size.
I'd bet I lose 100ms of processing time trying to weed out bad bots to protect core processes.
$ip=sprintf("%u",ip2long($ip));
if($ip>File1Start && $ip<File1End) {
$csvfilename="include/ipCC1.csv";
}
elseif($ip>File2Start && $ip<File2End) {
$csvfilename="include/ipCC2.csv";
}
$ip=sprintf("%u",ip2long($ip));
if($ip<File_Set_1_End) {
if($ip<File_1_End) {
$csvfilename="include/ipCC1.csv";
}
elseif($ip<File_2_End) {
$csvfilename="include/ipCC2.csv";
}
elseif($ip<File_3_End) {
$csvfilename="include/ipCC3.csv";
}
elseif($ip<File_4_End) {
$csvfilename="include/ipCC4.csv";
}
elseif($ip<File_5_End) {
$csvfilename="include/ipCC5.csv";
}
}
elseif($ip<File_Set_2_End) {
if($ip<File_6_End) {
$csvfilename="include/ipCC6.csv";
}
elseif($ip<File_7_End) {
$csvfilename="include/ipCC7.csv";
}
elseif($ip<File_8_End) {
$csvfilename="include/ipCC8.csv";
}
elseif($ip<File_9_End) {
$csvfilename="include/ipCC9.csv";
}
elseif($ip<File_10_End) {
$csvfilename="include/ipCC10.csv";
}
}
if(
($aIPClass['B']=="173.212." && $aIPParts[2]>=192) || //#*$!#*$! Network Operations Center Inc 2009-12-19
($aIPClass['B']=="173.45." && $aIPParts[2]>=64 && $aIPParts[3]<=127) || //Columbus #*$!x.#*$! Inc
($aIPClass['B']=="174.34." && $aIPParts[2]>=128 && $aIPParts[2]<=191) || //#*$!#*$!xservers.com
($aIPClass['B']=="194.110." && $aIPParts[2]>=160 && $aIPParts[2]<=163) || // #*$!#*$!x Host 2009-12-19
($aIPClass['B']=="194.110." && $aIPParts[2]>=160 && $aIPParts[2]<=163) || // #*$!#*$!xx Host 2009-12-19
($aIPClass['B']=="204.236." && $aIPParts[2]>=128) || // Amazon Web Services (dynamic hosting) 2009-12-22
...55 total lines
Anyway, just another thought while I'm sitting here thinking about how I would try to get to the right place in a smaller file faster and I'm sure you can see what I'm getting at...
if($ip<3259317248){
if($ip<1439557888){$csvfilename=$strRoot."include/WritableFiles/ip2country-1.csv";}
else{$csvfilename=$strRoot."include/WritableFiles/ip2country-2.csv";}
}else{
if($ip<3399547952){$csvfilename=$strRoot."include/WritableFiles/ip2country-3.csv";}
else{$csvfilename=$strRoot."include/WritableFiles/ip2country-4.csv";}
}
$fp=fopen($csvfilename,"r");
especially if the OS will get in the habit of keeping it in memory if it is called enough
[edited by: TheMadScientist at 11:35 pm (utc) on Feb 9, 2010]
Binary format may speed up the searching because it would take less space; 8 bytes for 2 IPs + 2 char country codes.
If I were to convert the IP addresses to binary format I'd need some automated conversion for that