PHP method to obtain the province and city based on the IP address_PHP tutorial

WBOY
Release: 2016-07-13 09:55:02
Original
1233 people have browsed it

php implements the method of obtaining the province and city based on the IP address

Today, let’s discuss how to obtain the user’s IP address in PHP, PHP determines the city where the user is located based on the IP, and PHP The problem of city switching or jumping based on IP.

How to obtain the geographical location (province, city, etc.) of an existing IP address

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

function GetIpLookup($ip = ''){

if(empty($ip)){

return '请输入IP地址';

}

$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?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('123.125.114.144'); //baidu.com IP地址

var_dump($ipInfos);

1

2

3

1

2

3

4

5

6

7

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());

4

5

6

7

8

9

10

11

12

13

14

15

17 18 19 20
function GetIpLookup($ip = ''){ if(empty($ip)){
return 'Please enter IP address';
} $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?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('123.125.114.144'); //baidu.com IP address var_dump($ipInfos);
Send a simplified version again  ?
1 2 3 4 5 6 7 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 realizes city switching or jump based on IP address At this point, the problem is actually very simple and can be solved with simple js. Paragraph C is as follows: // Jump to the specified page js to get the city based on the IP address var city=''; //Jump to the specified page based on IP address for all cities  if(city.indexOf("Shanghai City")>=0){ window.location.href="http://shanghai.demo.com/"; } Put the A section of code at the beginning and the C section of code above at the beginning and end of the B section of code respectively, and then we add the following code to the page that needs to be jumped:   Refresh the page. Did you achieve the desired effect? ​​ The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/993430.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/993430.htmlTechArticlephp implements the method of obtaining the province and city based on the IP address. Today, let’s discuss how to obtain the user’s IP in PHP Address, PHP determines the city where the user is located based on IP, and PHP implements city switching based on IP...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template