当前位置:Gxlcms > PHP教程 > php怎么得到本机的ip地址?O(∩_∩)O

php怎么得到本机的ip地址?O(∩_∩)O

时间:2021-07-01 10:21:17 帮助过:23人阅读

php如何得到本机的ip地址??O(∩_∩)O~
请问我用php命令执行php文件时,怎么得到本机的ip地址呢?原来通过$_SERVER[]不好用了,先谢谢了O(∩_∩)O~

------解决方案--------------------
如果你在自己电脑上运行的话无非就是 127.0.0.1
如果在虚拟空间或者服务器上运行的话就是 $_SERVER["SERVER_ADDR"]了
------解决方案--------------------
PHP code
 
echo "
";
?>


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 "
";
?>

方法二:
echo "
";
?>

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."
";
?>


方法三(最简单):
echo "
";
?>

$iipp = $_SERVER["REMOTE_ADDR"];
echo $iipp;
echo "
";

人气教程排行