- /**
- * Get city name based on IP address pure IP database
- * edit:bbs.it-home.org
- */
- function Convertip ($IP) {
- IP Data File path
- $dat _path = ' QQWry.Dat ';
- Check IP Address
- if (!filter_var ($ip, filter_validate_ip)) {
- Return ' IP address Error ';
- }
- Open IP Data file
- if (! $fd = @fopen ($dat _path, ' RB ')) {
- Return ' IP date file not exists or access denied ';
- }
- Decomposition IP for operation, resulting in shaping number
- $ipNum = Ip2long ($IP);
- Get IP Data index start and end locations
- $DataBegin = Fread ($FD, 4);
- $DataEnd = Fread ($FD, 4);
- $ipbegin = Implode (' ', Unpack (' L ', $DataBegin));
- if ($ipbegin < 0) $ipbegin + = POW (2, 32);
- $ipend = Implode (' ', Unpack (' L ', $DataEnd));
- if ($ipend < 0) $ipend + = POW (2, 32); $ipAllNum = ($ipend-$ipbegin)/7 + 1; $BeginNum = 0; $EndNum = $ipAllNum; Search for matching IP records from index records using binary lookup while ($ip 1num> $ipNum | | $ip 2num< $ipNum) {
- $Middle = Intval (($EndNum + $BeginNum)/2);
- Offset pointer to index position read 4 bytes
- Fseek ($FD, $ipbegin + 7 * $Middle);
- $ipData 1 = fread ($FD, 4);
- if (strlen ($ipData 1) < 4) {
- Fclose ($FD);
- Return ' System Error ';
- }
- Extracted data transformation growth shaping, if the data is negative, plus 2 of the power of 32
- $ip 1num = Implode (', Unpack (' L ', $ipData 1));
- if ($ip 1num < 0) $ip 1num + = POW (2, 32); Extract the number of long integers greater than our IP address then modify the end position to the next loop if ($ip 1num > $ipNum) {
- $EndNum = $Middle;
- Continue
- }
- Remove an index after the last index is taken
- $DataSeek = Fread ($FD, 3);
- if (strlen ($DataSeek) < 3) {
- Fclose ($FD);
- Return ' System Error ';
- }
- $DataSeek = Implode (', Unpack (' L ', $DataSeek. chr (0)));
- Fseek ($FD, $DataSeek);
- $ipData 2 = fread ($FD, 4);
- if (strlen ($ipData 2) < 4) {
- Fclose ($FD);
- Return ' System Error ';
- }
- $ip 2num = Implode (', Unpack (' L ', $ipData 2));
- if ($ip 2num < 0) $ip 2num + = POW (2, 32);
- No hints found
- if ($ip 2num < $ipNum) {
- if ($Middle = = $BeginNum) {
- Fclose ($FD);
- Return ' Unknown ';
- }
- $BeginNum = $Middle;
- }
- }
- The following code read dizzy, did not read clearly, interested in reading slowly
- $ipFlag = Fread ($FD, 1);
- if ($ipFlag = = Chr (1)) {
- $ipSeek = Fread ($FD, 3);
- if (strlen ($ipSeek) < 3) {
- Fclose ($FD);
- Return ' System Error ';
- }
- $ipSeek = Implode (', Unpack (' L ', $ipSeek. chr (0)));
- Fseek ($FD, $ipSeek);
- $ipFlag = Fread ($FD, 1);
- }
- if ($ipFlag = = Chr (2)) {
- $AddrSeek = Fread ($FD, 3);
- if (strlen ($AddrSeek) < 3) {
- Fclose ($FD);
- Return ' System Error ';
- }
- $ipFlag = Fread ($FD, 1);
- if ($ipFlag = = Chr (2)) {
- $AddrSeek 2 = fread ($FD, 3);
- if (strlen ($AddrSeek 2) < 3) {
- Fclose ($FD);
- Return ' System Error ';
- }
- $AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
- Fseek ($FD, $AddrSeek 2);
- } else {
- Fseek ($FD,-1, seek_cur);
- }
- while ($char = Fread ($FD, 1))! = Chr (0))
- $ipAddr 2. = $char;
- $AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));
- Fseek ($FD, $AddrSeek);
- while ($char = Fread ($FD, 1))! = Chr (0))
- $ipAddr 1. = $char;
- } else {
- Fseek ($FD,-1, seek_cur);
- while ($char = Fread ($FD, 1))! = Chr (0))
- $ipAddr 1. = $char;
- $ipFlag = Fread ($FD, 1);
- if ($ipFlag = = Chr (2)) {
- $AddrSeek 2 = fread ($FD, 3);
- if (strlen ($AddrSeek 2) < 3) {
- Fclose ($FD);
- Return ' System Error ';
- }
- $AddrSeek 2 = Implode ("', Unpack (' L ', $AddrSeek 2.chr (0)));
- Fseek ($FD, $AddrSeek 2);
- } else {
- Fseek ($FD,-1, seek_cur);
- }
- while ($char = Fread ($FD, 1))! = Chr (0)) {
- $ipAddr 2. = $char;
- }
- }
- Fclose ($FD);
- The result is returned after the corresponding substitution operation is finally made
- if (Preg_match ('/http/i ', $ipAddr 2)) {
- $ipAddr 2 = ";
- }
- $IPADDR = "$ipAddr 1 $ipAddr 2";
- $IPADDR = preg_replace ('/cz88.net/is ', ' ', $ipaddr);
- $IPADDR = preg_replace ('/^s*/is ', ' ', $ipaddr);
- $IPADDR = preg_replace ('/s*$/is ', ' ', $ipaddr);
- if (Preg_match ('/http/i ', $ipaddr) | | $ipaddr = = ") {
- $ipaddr = ' Unknown ';
- }
- return $ipaddr;
- }
- Header ("content-type:text/html; Charset=utf-8 ");
- $ip = $_server[' http_x_forwarded_for ');
- if (isset ($_get[' Q '))) {
- $ip = $_get[' Q '];
- if (Filter_var ("http://". $ip, filter_validate_url,filter_flag_host_required)) {
- $ip = gethostbyname ($IP);
- }
- }
- echo "$ip,". Mb_convert_encoding (Convertip ($IP), "Utf-8", "gb2312");
- ?>
Copy Code |