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