PHP query ip address location (province, city) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP queries the ip address location (province, city ). This article introduces how to use relevant api interfaces to obtain a php instance in the city based on the client IP address. let's take a look at it. Today, we will share a PHP client IP address. This article introduces how to use relevant api interfaces to obtain a php instance in the city or region based on the client IP address. let's take a look at it.

Today, I am sharing a PHP app that obtains the location of the app based on the client IP address. I have been using this app for a long time and I feel that this interface is still stable. Although there are many similar items on the Internet, I think this is quite good. The core code can be implemented in only three lines. Although the function is not very powerful, we usually use it. The following code is used:

The code is as follows:

Obtain the region through IP address


: Returned data


: Query successful


If you like it, you can try it and get a few lines of code. I think it is quite practical !!

This article from the generation of Ruibo customer original address: http://www.daixiaorui.com/read/3.html

// Obtain the IP address

// $ Ip = $ _ SERVER ['remote _ ADDR ']; // automatically obtains the IP address of the client.

// Region corresponding to the ip address

If (! Empty ($ _ POST ['IP']) {

$ Ip = $ _ POST ['IP'];

// Interface address, which returns an xml result set, 1;

$ Str = file_get_contents ("http://www.yodao.com/smartresult-xml/search.s? Type = ip & q = ". $ ip );

// Here we need to get the address information. There are many xml extraction methods, and I use regular expressions.

Preg_match_all ("/ (.*?) /", $ Str, $ addr1 );

// Final result, 2;

$ Addr = $ ip. "=>". $ addr1 [1] [0];

Echo $ addr;

}

?>


: Returned data


: Query successful


If you like it, you can try it and get a few lines of code. I think it is quite practical !!

Bytes. Today, I will share a PHP file to obtain the IP address of the client...

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.