php获取本机真实IP地址实例代码

yipeiwu_com6年前PHP代码库

本文实例为大家分享了php获取本机真实IP地址实例代码,供大家参考。

主要是获取操作系统为win2000/xp、win7的本机IP真实地址,和获取操作系统为linux类型的本机IP真实地址,具体内容如下

function getLocalIP() {
 $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]);
    }
   }
  }
 }
//获取操作系统为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]);
   }
  }
 }
 return '127.0.0.1';
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

PHP乱码问题,UTF-8乱码常见问题小结

一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: <meta http-equiv='Content-Type' content='text/html...

php上传图片类及用法示例

本文实例讲述了php上传图片类及用法。分享给大家供大家参考,具体如下: 1.类文件名为:upclass.php <?php class upclass{ public...

php封装的图片(缩略图)处理类完整实例

本文实例讲述了php封装的图片(缩略图)处理类。分享给大家供大家参考,具体如下: <?php //图片处理工具类 class Image{ //属性 p...

php 删除无限级目录与文件代码共享

<? //删除目录 class del_path { function wm_chief_delpath($del_path) { if(!file_exists($del_pat...

PHP 中文简繁互转代码 完美支持大陆、香港、台湾及新加坡

例子:(简 > 繁)面包 > 麵包 (zh-tw)寮国 > 老撾 (zh-hk)中国人寿 > 中國人壽 (zh-hk)罗纳尔多 > 朗拿度 (zh-hk)(...