最近突然发现本函数无法获取地址信息了,经查看file_get_contents函数无法获取IP138网页上的真实内容。
原因如下,由于IP138采用API接口,对网页查询进行了User_agent的限制。非正常请求返回空白,而默认PHP是不发送User_agent请求头的,需要对php.ini进行配置,清除php.ini中User_agent前面的分号,重启下apache即可。
================================================================

会员用户需要记录登录的实际地址,采用IP138的数据。

function get_ip($ip)
{
    if ($ip){
        $a="本站数据:";
        $url="http://www.ip138.com/ips138.asp?ip=$ip";
        $str=mb_convert_encoding(file_get_contents($url),"utf-8","GBK");//强制转换成UTF8编码
        preg_match('/<ul class="ul1"><li>(.*?)<\/li><li>/',$str,$result);//正则表达式查找
        $ip_str=str_replace($a,"",$result[1]);
        return $ip_str;
    }else{
        return '未知地址';
    }
}