PHP obtains the specific implementation of the city based on the IP address. File Directory: ipLocation ----- qqwry ---------- QQWry. Dat ----- ipCity. class. phpipCity. class. php file code: Copy the code as follows :? PhpclassipCity {*** obtain the file directory based on the IP address:
IpLocation
----- Qqwry
---------- QQWry. Dat
----- IpCity. class. php
IpCity. class. php file code:
The code is as follows:
Class ipCity {
/**
* Obtain the corresponding city based on the IP address
* @ Param type $ userip user IP address
* @ Return string
*/
Public function getCity ($ userip, $ dat_path = ''){
// IP database path. here, the pure version of qq ip database 20110405 is used.
Empty ($ dat_path) & $ dat_path = FCPATH. 'In in/ipLocation/qqwry/QQWry. Dat ';
// Determine whether the IP address is valid
If (preg_match ("/^ ([0-9] {1, 3 }.) {3} [0-9] {1, 3} $/", $ userip) = 0 ){
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 ';
} Elseif (! $ This-> is_utf8 ($ useripaddr )){
$ Useripaddr = iconv ('gbk', 'utf-8', $ useripaddr );
}
Return $ useripaddr;
}
/**
* Determine whether my UTF-8 encoded string is used
* @ Param type $ string
* @ Return boolean
*/
Private function is_utf8 ($ string ){
If (preg_match ("/^ ([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1}/", $ string) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1} $/", $ string) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {2,}/", $ string) = true ){
Return true;
} Else {
Return false;
}
}
}
QQWry. Dat file: http://xiazai.jb51.net/201311/yuanma/qqwry.dat (jb51.net.zip
Demo:
The code is as follows:
Include FCPATH. 'In in/ipLocation/ipCity. class. php ';
$ City = new ipCity ();
$ Addr = $ city-> getCity ('2017. 0.0.1 ');
Echo $ addr; // echo local address
Export ipLocation ----- qqwry ---------- QQWry. Dat ----- ipCity. class. php ipCity. class. php file code :? Php class ipCity {/*** obtained according to the IP address...