|
Function getip ()
{
If (getenv ('http _ client_ip ')){
$ Ip = getenv ('http _ client_ip ');
} Else if (getenv ('http _ x_forwarded_for ')){
$ Ip = getenv ('http _ x_forwarded_for ');
} Else if (getenv ('remote _ addr ')){
$ Ip = getenv ('remote _ addr ');
} Else {
$ Ip = $ _ server ['remote _ addr '];
}
Return $ ip;
}
Function getipaddres ($ ip = '')
{
If ($ ip = ''){
$ Ip = getip ();
}
If ($ ip = '2014. 0.0.1 '){
Return $ ip_addres = 'local machine ';
}
$ Recordlength = 17 + 22 + 13 + 47 + 12 + 1;
$ Ret = ereg ('^ ([0-9] + ). ([0-9] + ). ([0-9] + ). ([0-9] +) $ ', $ ip, $ ips tutorial ection );
If ($ ret = false) {$ ip_addres = 'IP Address Error ';}
For ($ I = 1; $ I <= 4; $ I ++ ){
If ($ ipsection [$ I]> 255 ){
$ Ip_addres = 'IP Address Error ';
} Else {
$ Thisip. = sprintf ('% 03.0f', $ ipsection [$ I]). ($ I <4 )? '.':'');
}
}
$ Fp = fopen ($ obj ['rootdir']. 'Data/ip.txt ', 'RB ');
If ($ fp = null ){
$ Ip_addres = 'IP record file opening error ';
}
Fseek ($ fp, 0, seek_end );
$ Datafieldbegin = 0xc2;
$ Recordcount = floor (ftell ($ fp)-$ datafieldbegin)/$ recordlength );
If ($ recordcount <= 1 ){
$ Ip_addres = 'IP record file opening error ';
} Else {
$ Rangb = 0;
$ Range = $ recordcount;
While ($ rangb <$ range-1 ){
$ Recno = floor ($ rangb + $ range)/2 );
Fseek ($ fp, $ recno * $ recordlength + $ datafieldbegin, seek_set );
$ Buf = fread ($ fp, $ recordlength );
If (strlen ($ buf) = 0 ){
Return false;
}
$ Startip = (substr ($ buf, 0, 17 ));
$ Endip = trim (substr ($ buf, 17, 22 ));
$ Country = trim (substr ($ buf, 17 + 22, 13 ));
$ Local = trim (substr ($ buf, 17 + 22 + 13, 47 ));
If (strcmp ($ thisip, $ startip) >=0 & strcmp ($ thisip, $ endip) <= 0 ){
Break;
}
If (strcmp ($ thisip, $ startip)> 0 ){
$ Rangb = $ recno;
} Else {
$ Range = $ recno;
}
}
If (! ($ Rangb <$ range-1 )){
$ Ip_addres = 'unknown address! ';
} Else {
$ Ip_addres = $ country;
$ Ip_addres. = $ local;
}
}
Fclose ($ fp );
Return $ ip_addres;
} |