1,PHP的全局变量中,有提供获取服务器IP的变量
$_SERVER['SERVER_ADDR'];
但是,这只是针对从浏览器请求过来的;
如果是通过php-cli命令行方式,是没有这个参数的
2,通过获取服务器hostname来得到ip
$host= gethostname(); $ip = gethostbyname($host);
但是,前提条件是服务器配置了/etc/hosts
3,通过ifconfig获取真实ip
$command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"; $localIP = exec ($command); echo $localIP;
但是,遇到多网卡不一定是取eth0
总结,没有十全十美的方法,根据自己环境选取吧
Leave a Reply