Php: how to obtain the province and city based on the ip address. tutorial on ip address province and city _ PHP

Source: Internet
Author: User
Php implements the method of obtaining the province and city where the ip address is located based on the ip address. Php implements the function of obtaining the province and city based on the ip address, and functionGetIpLookup ($ ip) for the province and city based on the existing ip address) {if (e php implements the method of obtaining the province and city based on the ip address, the province and city where the ip address is located

How to obtain the geographic location (province, city, etc.) based on the existing IP address

Function GetIpLookup ($ ip = '') {if (empty ($ ip) {return 'Enter ip address';} $ res = @ file_get_contents ('http: // response? Format = js & ip = '. $ ip); if (empty ($ res) {return false;} $ jsonMatches = array (); preg_match ('#\{. +? \}# ', $ Res, $ jsonMatches); if (! Isset ($ jsonMatches [0]) {return false;} $ json = json_decode ($ jsonMatches [0], true); if (isset ($ json ['ret ']) & $ json ['ret '] = 1) {$ json ['IP'] = $ ip; unset ($ json ['ret']);} else {return false;} return $ json;} $ ipInfos = GetIpLookup ('2017. 125.114.144 '); // baidu.com IP address var_dump ($ ipInfos );

Release a simplified version

function getIpAddress(){    $ipContent  = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js");    $jsonData = explode("=",$ipContent);     $jsonAddress = substr($jsonData[1], 0, -1);    return $jsonAddress;  }  $ip_info=json_decode(getIpAddress());  

PHP implements City switching or redirection based on IP addresses

Here, the problem is actually quite simple. it can be done with simple js. Section C is as follows:

// Jump to the specified page based on the IP address. js gets the city

Var city = '<? Echo ipCity ($ xp_UserIp);?> '; // Jump to the specified page according to the IP address of all cities

If (city. indexOf ("Shanghai")> = 0 ){

Window. location. href = "http://shanghai.demo.com /";}
Place the-segment code and the C-segment code above in the header and end of the B-segment code respectively, and then add the following code on the page to jump:

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.