<? /* Function Name: ipCity Parameter description: $ userip -- user IP Address Function: PHP uses IP addresses to determine the city in which a user is located. Author: lee Contact: xpsem2010@gmail.com */ Function ipCity ($ userip ){ // IP database path. Here, the pure version of qq ip database 20110405 is used. $ Dat_path = 'qqwry. dat '; // Determine whether the IP address is valid If (! Ereg ("^ ([0-9] {1, 3}.) {3} [0-9] {1, 3} $", $ userip )){ Return 'IP Address invalid '; } // Open the IP database If (! $ Fd = @ fopen ($ dat_path, 'rb ')){ Return 'IP data file not exists or access denied '; } // The explode function breaks down the IP address and returns an integer. $ Userip = explode ('.', $ userip ); $ UseripNum = $ userip [0] x 16777216 + $ userip [1] * 65536 + $ userip [2] * 256 + $ userip [3]; // Obtain the start and end positions of the IP address index. $ DataBegin = fread ($ fd, 4 ); $ DataEnd = fread ($ fd, 4 ); $ Useripbegin = implode ('', unpack ('l', $ DataBegin )); If ($ useripbegin <0) $ useripbegin + = pow (2, 32 ); $ Useripend = implode ('', unpack ('l', $ DataEnd )); If ($ useripend <0) $ useripend + = pow (2, 32 ); $ UseripAllNum = ($ useripend-$ useripbegin)/7 + 1; $ BeginNum = 0; $ EndNum = $ useripAllNum; // Use the Binary Search Method to search for matched IP address records from index records While ($ userip1num> $ useripNum | $ userip2num <$ useripNum ){ $ Middle = intval ($ EndNum + $ BeginNum)/2 ); // Read 4 bytes from the offset pointer to the index position Fseek ($ fd, $ useripbegin + 7 * $ Middle ); $ UseripData1 = fread ($ fd, 4 ); If (strlen ($ useripData1) <4 ){ Fclose ($ fd ); Return 'file error '; } // The extracted data is converted into an integer. If the data is negative, the power of 2 is added. $ Userip1num = implode ('', unpack ('l', $ useripData1 )); If ($ userip1num <0) $ userip1num + = 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 ($ userip1num> $ useripNum ){ $ EndNum = $ Middle; Continue; } // Obtain the next index after obtaining the previous index $ DataSeek = fread ($ fd, 3 ); If (strlen ($ DataSeek) <3 ){ Fclose ($ fd ); Return 'file error '; } $ DataSeek = implode ('', unpack ('l', $ DataSeek. chr (0 ))); Fseek ($ fd, $ DataSeek ); $ UseripData2 = fread ($ fd, 4 ); If (strlen ($ useripData2) <4 ){ Fclose ($ fd ); Return 'file error '; } $ Userip2num = implode ('', unpack ('l', $ useripData2 )); If ($ userip2num <0) $ userip2num + = pow (2, 32 ); // Unable to find the IP address corresponding to the city If ($ userip2num <$ useripNum ){ If ($ Middle = $ BeginNum ){ Fclose ($ fd ); Return 'no data '; } $ BeginNum = $ Middle; } } $ UseripFlag = fread ($ fd, 1 ); If ($ useripFlag = chr (1 )){ $ UseripSeek = fread ($ fd, 3 ); If (strlen ($ useripSeek) <3 ){ Fclose ($ fd ); Return 'System error '; } $ UseripSeek = implode ('', unpack ('l', $ useripSeek. chr (0 ))); Fseek ($ fd, $ useripSeek ); $ UseripFlag = fread ($ fd, 1 ); } If ($ useripFlag = chr (2 )){ $ AddrSeek = fread ($ fd, 3 ); If (strlen ($ AddrSeek) <3 ){ Fclose ($ fd ); Return 'System error '; } $ UseripFlag = fread ($ fd, 1 ); If ($ useripFlag = 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 )) $ UseripAddr2. = $ char; $ AddrSeek = implode ('', unpack ('l', $ AddrSeek. chr (0 ))); Fseek ($ fd, $ AddrSeek ); While ($ char = fread ($ fd, 1 ))! = Chr (0 )) $ UseripAddr1. = $ char; } Else { Fseek ($ fd,-1, SEEK_CUR ); While ($ char = fread ($ fd, 1 ))! = Chr (0 )) $ UseripAddr1. = $ char; $ UseripFlag = fread ($ fd, 1 ); If ($ useripFlag = 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 )){ $ UseripAddr2. = $ char; } } Fclose ($ fd ); // Return the city result corresponding to the IP address If (preg_match ('/http/I', $ useripAddr2 )){ $ UseripAddr2 = ''; } $ Useripaddr = "$ useripAddr1 $ useripAddr2 "; $ Useripaddr = preg_replace ('/CZ88.Net/is', '', $ useripaddr ); $ Useripaddr = preg_replace ('/^ s */is', '', $ useripaddr ); $ Useripaddr = preg_replace ('/s * $/is', '', $ useripaddr ); If (preg_match ('/http/I', $ useripaddr) | $ useripaddr = ''){ $ Useripaddr = 'no data '; } Return $ useripaddr; } ?> |