请问我用php命令执行php文件时,怎么得到本机的ip地址呢?原来通过$_SERVER[]不好用了,先谢谢了O(∩_∩)O~
------解决方案--------------------
如果你在自己电脑上运行的话无非就是 127.0.0.1
如果在虚拟空间或者服务器上运行的话就是 $_SERVER["SERVER_ADDR"]了
------解决方案--------------------
- PHP code
<?php
echo " <br>";
?>
<?php
function GetIP()
{
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
$cip = $_SERVER["HTTP_CLIENT_IP"];
else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if(!empty($_SERVER["REMOTE_ADDR"]))
$cip = $_SERVER["REMOTE_ADDR"];
else
$cip = "无法获取!";
return $cip;
}
echo " <br>";
?>
方法二:
<?php
echo " <br>";
?>
<?
error_reporting (E_ERROR | E_WARNING | E_PARSE);
if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
{
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
{
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
{
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR"))
{
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR"))
{
$ip = getenv("REMOTE_ADDR");
}
else
{
$ip = "Unknown";
}
echo "你的IP地址是:".$ip." <br>";
?>
方法三(最简单):
<?php
echo " <br>";
?>
<?
$iipp = $_SERVER["REMOTE_ADDR"];
echo $iipp;
echo " <br>";