PHP implementation method based on IP address to obtain its province _php tutorial

Source: Internet
Author: User

PHP implementation of the method based on the IP address to obtain its local province


Today, we will discuss how to get the User IP address in PHP, PHP according to the IP to determine the user's city and PHP based on the IP to achieve city switching or jump problem.

Ways to get their geographic location (provinces, cities, etc.) based on existing IP addresses

?

1

2

3

4

5

6

7

8

9

11

+

+

/+

/

/+

+

Funct Ion 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);

and send a simplified version of

?

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 enables city switching or jumping based on IP address

Here, in fact, the problem is very simple, with simple JS on all. Paragraph c is as follows:

Jump to specified page according to IP address JS get the city

var city= '; Jump to a specific page based on IP address for all cities

if (City.indexof ("Shanghai City") >=0) {

Window.location.href= "http://shanghai.demo.com/"; }

Put the section a code at the beginning and the C code above in the head and tail of the B-segment code, and then we add the following code to the page where we need to jump:

  

Refresh the page, is not to achieve the desired effect?

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/993430.html www.bkjia.com true http://www.bkjia.com/PHPjc/993430.html techarticle PHP Implementation based on the IP address to obtain its province of the method today, discuss how to get the User IP address in PHP, PHP according to the IP to determine the user's city and PHP based on IP to achieve city cut ...

  • 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.