首页 > 其他 > 详细

获取本机IP非127.0.0.1

时间:2016-04-17 20:45:48      阅读:172      评论:0      收藏:0      [点我收藏+]

protected function GetiP()
    {
    $preg="/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
    //获取操作系统为win2000/xp、win7的本机IP真实地址
    exec("ipconfig",$out,$stats);
        if(!empty($out))
        {
            foreach($out AS $row)
            {
                if(strstr($row,"IP") && strstr($row,":") && !strstr($row,"IPv6"))
                {
                $tmpIp = explode(":", $row);
                    if(preg_match($preg,trim($tmpIp[1])))
                    {
                        return trim($tmpIp[1]);
                    }
                }
            }
        }        
    }
    public function actionGetips(){
        $ip = $this->Getip();
        echo $ip;
    }

 

//获取操作系统为linux类型的本机IP真实地址
exec("ifconfig",$out,$stats);
if(!empty($out))
{
    if(isset($out[1]) && strstr($out[1],‘addr:‘))
    {
        $tmpArray = explode(":", $out[1]);
        $tmpIp = explode(" ", $tmpArray[1]);
        if(preg_match($preg,trim($tmpIp[0])))
        {
            return trim($tmpIp[0]);
        }
    }
}

获取本机IP非127.0.0.1

原文:http://www.cnblogs.com/jhy-ocean/p/5401914.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!