How does php obtain the client ip address and province?

Source: Internet
Author: User
How does php obtain the client ip address and the specific implementation code of the province and city .. Thank you..., provide several IP address API & nbsp; Sina IP address query interface: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js Sina another ip address query interface: http: // counte php how to obtain the client ip address and the province
Find the specific implementation code .. Thank you ..

------ Solution --------------------

Provides several IP address APIs

Sina IP address query interface: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js
Sina another IP address query interface: http://counter.sina.com.cn/ip
Sina multi-region Test Method: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = 218.192.3.42
Sohu IP address query interface (default GBK): http://pv.sohu.com/cityjson
Sohu IP address query interface (can be set encoding): http://pv.sohu.com/cityjson? Ie = UTF-8
Sohu another IP address query interface: http://txt.go.sohu.com/ip/soip
------ Solution --------------------
1. you can build your own ip database, and the data can be extracted from data packets similar to innocence.

2. get the ip Code. copy one of the following code on the Internet:

Function getip ()
{
If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
{
$ Ip = getenv ("HTTP_CLIENT_IP ");
} Else
If (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR "),
"Unknown "))
{
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
} Else
If (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
{
$ Ip = getenv ("REMOTE_ADDR ");
} Else
If (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'],
"Unknown "))
{
$ Ip = $ _ SERVER ['remote _ ADDR '];
} Else
{
$ Ip = "unknown ";
}
Return ($ ip );
}

3. you can pass the ip address to some interfaces available on the Internet to retrieve the returned data for resolution.

For example
Http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = 219.136.252.33

Note that the visitor's ip address is automatically obtained if no ip address is transferred.

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.