悠闲博客-blog.yxrjt.cn

【PHP】获取用户真实【ip归属地】【手机号码归属地】

更新时间:2025-10-14 09:44点击:71

<?php
class Getipclass{
/**
 * 获取用户真实 IP
 */
function getIP()
{
    static $realip;
    if (isset($_SERVER)){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}

    
  //获取ip归属地
  function get_ip_city($ip){ 
    $ch = curl_init(); 
    $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip='.$ip; //用curl发送接收数据 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //请求为https 
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $location = curl_exec($ch); curl_close($ch); 
    //转码 
    $location = mb_convert_encoding($location, 'utf-8','GB2312'); 
    //var_dump($location); 
    //截取{}中的字符串 
    $location = substr($location, strlen('({')+strpos($location, '({'),(strlen($location) - strpos($location, '})'))*(-1)); 
    //将截取的字符串$location中的‘,’替换成‘&’   将字符串中的‘:‘替换成‘=’ 
    $location = str_replace('"',"",str_replace(":","=",str_replace(",","&",$location))); 
    //php内置函数,将处理成类似于url参数的格式的字符串  转换成数组 
    parse_str($location,$ip_location); 
    return $ip_location['addr'];
  }
  
  //手机号码归属地
  function getMobileInfo($mobile){ 
   
        $phone_json = file_get_contents('http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={'.$mobile.'}&resource_id=6004&ie=utf8&oe=utf8&format=json'); 
        $phone_array = json_decode($phone_json,true); 
        $phone_info = array(); 
        $phone_info['mobile'] = $mobile; 
        $phone_info['type'] = $phone_array['data'][0]['type']; 
        $phone_info['location'] = $phone_array['data'][0]['prov'].$phone_array['data'][0]['city']; 
        
        return $phone_info;
       
  }

  

  
}

?>

栏目分类

联系方式
  • help@yxrjt.cn
  • lgc@yxrjt.cn
  • admin@yxrjt.cn