本文介绍了使用PHP从ifconfig获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下一个PHP代码:
I have the next php code:
<?php
$ip = shell_exec("/sbin/ifconfig | grep 'inet:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'");
echo $ip;
?>
从命令行运行它时工作正常( $ php5 ip .php
),但是当我从浏览器运行它时它什么都没有显示( http://localhost/ip.php
)。
It works fine when I run it from the command line ($php5 ip.php
), but when I run it from my browser it shows nothing (http://localhost/ip.php
).
顺便说一句,我正在尝试打印我的IP地址,但每当我使用 $ _ SERVER ['SERVER_ADDR'];
我得到 127.0.0.1
。
By the way, I'm trying to print my IP address but whenever I use $_SERVER['SERVER_ADDR'];
I get 127.0.0.1
.
推荐答案
它将在没有冒号的情况下工作到'inet'
It will work without colon next to 'inet'
grep 'inet '
这篇关于使用PHP从ifconfig获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!