PHP: Obtain the City Based on the IP address

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to go to if ($ useripFlagchr (2) {$ AddrSeekfread ($ fd, 3); if (strlen ($ AddrSeek) 3) {fclose ($ fd); returnSystemError;} $ useripFlagfread ($ fd, 1); if

Welcome to the Linux community forum and interact with 2 million technical staff> go to if ($ useripFlag = chr (2) {$ AddrSeek = fread ($ fd, 3 ); if (strlen ($ AddrSeek) 3) {fclose ($ fd); return 'System error';} $ useripFlag = fread ($ fd, 1); if

Welcome to the Linux community forum and interact with 2 million technicians>

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;

}

}

}

You can search for the QQWry. Dat file on Baidu or send it to your mailbox. I will send it to you via email.

Demo:

?

1

2

3

4 include FCPATH. 'In in/ipLocation/ipCity. class. php ';

$ City = new ipCity ();

$ Addr = $ city-> getCity ('2017. 0.0.1 ');

Echo $ addr; // echo local address

[1] [2]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.