Today, we will discuss how to obtain the User IP address in PHP, PHP based on IP to determine the user's city and PHP based on IP to achieve city switching or jump problem.
How to obtain the location (province, city, etc.) based on the 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 ' 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.
?
| 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 to achieve city switching or jump based on IP address
Here, in fact, the problem has been very simple, with simple JS will be all done. Paragraph C reads as follows:
Jump the specified page based on the IP address JS get the city
var city= ' ;///per IP address all cities jump to specified page
if (City.indexof ("Shanghai City") >=0) {
Window.location.href= "http://shanghai.demo.com/"; }
Place the first section A and the C section of code on the head and tail of code B, and then we add the following code to the page where we want to jump:
Refresh the page, is not to achieve the desired effect?
The above mentioned is the entire content of this article, I hope you can enjoy.