php获取本机IP地址
2018-04-02 22:04
190 查看
<?php function get_local_ip() { $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]))) { returntrim($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]))) { returntrim($tmpIp[0]); } } } return '127.0.0.1'; }
<?php /**获取服务器ip地址 * @return array|false|string */ function get_server_ip() { $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/"; $os = Check_Domain_Helper::check_machine_os(); if ($os === 'Windows') { //获取操作系统为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]); } } } } } else { //获取操作系统为linux类型的本机IP真实地址 $result = shell_exec("/sbin/ifconfig"); if (preg_match_all("/inet (\d+\.\d+\.\d+\.\d+)/", $result, $match) !== 0) // 这里根据你机器的具体情况, 可能要对“inet ”进行调整, 如“addr:”,看如下注释掉的if // if (preg_match_all("/addr:(\d+\.\d+\.\d+\.\d+)/", $result, $match) !== 0) { foreach ($match [0] as $k => $v) { if ($match [1] [$k] != "127.0.0.1") { $the_local_ip = $match [1] [$k]; return $match [1] [$k]; } } } } return '127.0.0.1'; }
<?php /**获取服务器ip地址 * @return array|false|string */ public static function get_server_ip() { $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/"; $os = Check_Domain_Helper::check_machine_os(); if ($os === 'Windows') { //获取操作系统为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]); } } } } } else { //获取操作系统为linux类型的本机IP真实地址 $match = ''; exec("ifconfig", $result, $stats); $result = implode("", $result); $is_match = preg_match_all("/addr:(\d+\.\d+\.\d+\.\d+)/", $result, $match); if ($is_match == 0) { $is_match = preg_match_all("/inet (\d+\.\d+\.\d+\.\d+)/", $result, $match); } var_dump($match); if ($is_match !== 0) { foreach ($match [0] as $k => $v) { if ($match [1] [$k] != "127.0.0.1") { $the_local_ip = $match [1] [$k]; return $match [1] [$k]; } } } } return '127.0.0.1'; }
相关文章推荐
- PHP根据IP获取当前所在地地址
- 淘宝IP地址库API接口 (PHP)通过ip获取地址信息
- php中获取指定IP的物理地址的代码(正则表达式)
- php获取用户IP地址
- php获取ip 地址
- PHP获取当前页面的名称,主机名,URL完整地址,URL参数,获取IP
- PHP基于新浪IP库获取IP详细地址的方法
- php获取浏览器信息。获取访客语言。获取访客操作系统。获取访客IP地址。获取访客地理位置。
- php获取浏览器信息。获取访客语言。获取访客操作系统。获取访客IP地址。获取访客地理位置。
- php 获取 ip 地址 函数
- 在PHP中获取IP来源地址并且插入数据库
- php获取用户ip地址,及格式转化存储
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
- 淘宝IP地址库API接口(PHP)通过ip获取地址信息
- 在PHP中获取IP来源地址并且插入数据库
- php获取页面地址(带GET参数)、客户端ip、服务器端ip
- PHP中获取用户IP地址
- php获取ip所在地址
- php 获取局域网的 所有用户的电脑IP和主机名、及mac地址
- php获取用户ip地址及城市名称