Js to locate the user's city information based on the IP address

Source: Internet
Author: User

1. Call the Sina IP Address Library

Sina provides open IP address library data for developers to call. Call address:

Http://int.dpool.sina.com.cn/iplookup/iplookup.php, you can return the provincial and municipal information of the current ipip.

<Script type = "text/javascript" src = "js/jquery. js"> </script>
<Script type = "text/javascript" src = "js/jquery. cityselect. js"> </script>
<Script type = "text/javascript" src = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?
Format = js "> </script>
We first load the jquery library and cityselect city drop-down plug-ins, then call Sina's IP address library, and return it in js format, of course, if you want to query the city information of a specified IP address, you can use an interface address such as: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = 123.123.123.123, replace the parameter ip value with the specified ip address.

Local js call:

Var myprovince = remote_ip_info ['Province '];
Var mycity = remote_ip_info ['city']
Var mydistrict = remote_ip_info ['District '];
$ (Function (){
$ ("# City_1"). citySelect ({
Prov: myprovince,
City: mycity
});
});
The HTML code is:

<H3> call the Sina IP library interface <P> your city is: <script> document. write (myprovince + ''+ mycity); </script> </p>
<Div id = "city_1">
<Select class = "prov"> </select>
<Select class = "city"> </select>
</Div>


2. Call the Taobao IP Address Library

Taobao also provides more authoritative IP Address Library, call address: http://ip.taobao.com/service/getIpInfo.php? Ip address = 123.123.123.123, return the province and city information of the corresponding IP address.

Call method:

$ (Function (){
$. GetJSON ("getTaoIP. php", function (json ){
Var myprovince2 = json. data. region;
Var mycity2 = json. data. city;
$ ("# City_2" ).html ("your city is:" + myprovince2 + mycity2 );
});
});
GetTaoIP. php is used to obtain the province and city information of Taobao's corresponding IP address. The returned data is in json format.

$ Ip = get_client_ip (); // obtain the ip address of the current user
$ Url = "http://ip.taobao.com/service/getIpInfo.php? Ip = ". $ ip;
$ Data = file_get_contents ($ url); // call the Taobao API to obtain information.
Echo $ data;
Get_client_ip () is used to obtain the IP address of a local user.

// Obtain the user's real IP address
Function get_client_ip (){
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 );
}

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.