How PHP obtains the IP of the client and the province

Source: Internet
Author: User
How PHP obtains the client's IP and the local province
For specific implementation code. Thank you..

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

Provides several IP address APIs

Sina's 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 code): Http://pv.sohu.com/cityjson?ie=utf-8
Sohu additional IP Address query interface: HTTP://TXT.GO.SOHU.COM/IP/SOIP
------Solution--------------------
1. You can build your own IP library, data can be extracted from a packet like innocence

2. Get the IP code online a whole bunch of copies here one

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. The IP can be passed to some of the currently available online interface to obtain the data returned to parse it

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

Note the IP address of the visitor is automatically obtained if the IP is not delivered.
  • 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.