第一步:将下列代码放置到主题模板函数 functions.php 闭合中:

//获取用户IP地址
function getip(){
 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;
}
// 新浪IP查询接口获取IP所在地
function getlocation($ip=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\') {
 empty($ip) && $ip = getip();
 if($ip==\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"127.0.0.1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") return \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"本机地址\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";	
 $api = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"; 
 $json = @file_get_contents($api); 
 $arr = json_decode($json,true);
 $country = $arr[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'country\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']; 
 $province = $arr[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'province\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']; 
 $city = $arr[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'city\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']; 
 if((string)$country == \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"中国\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"){
  if((string)($province) != (string)$city){
$_location = $province.$city;
  }else{
$_location = $country.$city;
  }
 }else{
  $_location = $country;
 }
 return $_location;
}

第二步:输出用户地理位置

<?php echo getlocation();?>

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:< a href="https://wpa.qq.com/msgrd?uin=30115776" target="_blank" rel="noopener noreferrer">点击这里给我发消息</ a>

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部