- /**
- * Obtain the city name pure IP database based on the IP address
- * Edit: bbs.it-home.org
- */
- Function convertip ($ ip ){
- // IP data file path
- $ Dat_path = 'qqwry. Dat ';
-
- // Check the IP address
- If (! Filter_var ($ ip, FILTER_VALIDATE_IP )){
- Return 'IP Address error ';
- }
- // Open the IP data file
- If (! $ Fd = @ fopen ($ dat_path, 'RB ')){
- Return 'IP date file not exists or access denied ';
- }
-
- // Calculate the number of integers by decomposing IP addresses.
- $ IpNum = ip2long ($ ip );
-
- // Obtain the start and end locations of the IP address data index.
- $ 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; // use the binary search method to search for matched IP records from the index records while ($ ip1num> $ ipNum | $ ip2num <$ ipNum ){
- $ Middle = intval ($ EndNum + $ BeginNum)/2 );
-
- // Read 4 bytes from the offset pointer to the index position
- Fseek ($ fd, $ ipbegin + 7 * $ Middle );
- $ IpData1 = fread ($ fd, 4 );
- If (strlen ($ ipData1) <4 ){
- Fclose ($ fd );
- Return 'system error ';
- }
- // The extracted data is converted into an integer. if the data is negative, the power of 2 is added.
- $ Ip1num = implode ('', unpack ('L', $ ipData1 ));
- If ($ ip1num <0) $ ip1num + = pow (2, 32 ); // if the number of extracted long integers is greater than our IP address, modify the end position for the next loop if ($ ip1num> $ ipNum ){
- $ EndNum = $ Middle;
- Continue;
- }
-
- // Obtain the next index after obtaining the previous index
- $ DataSeek = fread ($ fd, 3 );
- If (strlen ($ DataSeek) <3 ){
- Fclose ($ fd );
- Return 'system error ';
- }
- $ DataSeek = implode ('', unpack ('L', $ DataSeek. chr (0 )));
- Fseek ($ fd, $ DataSeek );
- $ IpData2 = fread ($ fd, 4 );
- If (strlen ($ ipData2) <4 ){
- Fclose ($ fd );
- Return 'system error ';
- }
- $ Ip2num = implode ('', unpack ('L', $ ipData2 ));
- If ($ ip2num <0) $ ip2num + = pow (2, 32 );
-
- // Unknown prompt not found
- If ($ ip2num <$ ipNum ){
- If ($ Middle = $ BeginNum ){
- Fclose ($ fd );
- Return 'unknown ';
- }
- $ BeginNum = $ Middle;
- }
- }
-
- // The following code is dizzy. if you do not understand it, read it 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 )){
- $ AddrSeek2 = fread ($ fd, 3 );
- If (strlen ($ AddrSeek2) <3 ){
- Fclose ($ fd );
- Return 'system error ';
- }
- $ AddrSeek2 = implode ('', unpack ('L', $ AddrSeek2.chr (0 )));
- Fseek ($ fd, $ AddrSeek2 );
- } Else {
- Fseek ($ fd,-1, SEEK_CUR );
- }
-
- While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
- $ IpAddr2. = $ char;
-
- $ AddrSeek = implode ('', unpack ('L', $ AddrSeek. chr (0 )));
- Fseek ($ fd, $ AddrSeek );
-
- While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
- $ IpAddr1. = $ char;
- } Else {
- Fseek ($ fd,-1, SEEK_CUR );
- While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
- $ IpAddr1. = $ char;
-
- $ IpFlag = fread ($ fd, 1 );
- If ($ ipFlag = chr (2 )){
- $ AddrSeek2 = fread ($ fd, 3 );
- If (strlen ($ AddrSeek2) <3 ){
- Fclose ($ fd );
- Return 'system error ';
- }
- $ AddrSeek2 = implode ('', unpack ('L', $ AddrSeek2.chr (0 )));
- Fseek ($ fd, $ AddrSeek2 );
- } Else {
- Fseek ($ fd,-1, SEEK_CUR );
- }
- While ($ char = fread ($ fd, 1 ))! = Chr (0 )){
- $ IpAddr2. = $ char;
- }
- }
- Fclose ($ fd );
-
- // Return the result after the replacement operation is completed.
- If (preg_match ('/http/I', $ ipAddr2 )){
- $ IpAddr2 = '';
- }
- $ Ipaddr = "$ ipAddr1 $ ipAddr2 ";
- $ 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 ");
-
- ?>
|