常用的两个PHP类

 

 
/**
 * Class Interval
 * @author logonmy
 * @desc  简单分析程序执行时间;
 */
 
Class Interval{
    var $start;
 
    public function getTrueTime()
    {
        list($sec,$unix) = explode(' ',microtime());
        return (float)$unix+(float)$sec;
    }
 
    public function __construct()
    {
        $this->start = $this->getTrueTime();
    }
 
    public function spend()
    {
        return round($this->getTrueTime() - $this->start,4);
    }
}
 
 
Class GetIpToCity{
    var $city;//获取ip对应城市,本单位网络平均用时0.3秒;
 
    public function __construct()
    {
        $this->city = $this->get_ip_to_city();
    }
 
    public function get_ip_to_city()
    {
        $city = '';
        $ip = $this->getip();
        $ip_api = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip';
        $json_data = file_get_contents($ip_api.$ip);
        $ip_data = json_decode($json_data,true);
        $city = $ip_data['city'];
        return $city;
    }
 
    ////获得访客真实ip
    public function getip(){
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理ip
            $ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
        }
        if($ip){
            $ips = array_unshift($ips,$ip);
        }
        $count = count($ips);
        for($i=0;$i<$count;$i++){
            if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网ip
                $ip = $ips[$i];
                break;
            }
        }
        $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
        if($tip=="127.0.0.1"){ //获得本地真实IP
            return $this->get_onlineip();
        }else{
            return $tip;
        }
    }
 
    ////获得本地真实IP
    public function get_onlineip() {
        $mip = file_get_contents("http://iframe.ip138.com/ic.asp");
        if($mip){
            preg_match("/\[.*\]/",$mip,$sip);
            $p = array("/\[/","/\]/");
            return preg_replace($p,"",$sip[0]);
        }else{return null;}
    }
 
 
 
}

posted on 2013-11-12 09:54  logon  阅读(308)  评论(0编辑  收藏  举报