首页 > 解决方案 > 如何打印您所在网站的“公共”IP 地址?

问题描述

到目前为止,我已经尝试过:

echo gethostbyname(gethostname());

但这给了我“本地”IP(不确定这是否是正确的术语)127.0.0.1,而我想要“公共”IP 地址。

例如,对于我151.101.193.69在使用网站服务https://ipinfo.info时获得的网站 stackoverflow.com ......这就是我正在寻找的。

标签: phpiphost

解决方案


使用此代码,

function getIPAddress() {  
 //whether ip is from the share internet
 if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  
            $ip = $_SERVER['HTTP_CLIENT_IP'];  
    }  
 //whether ip is from the proxy 
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
 }  
//whether ip is from the remote address 
else{  
         $ip = $_SERVER['REMOTE_ADDR'];  
 }  
 return $ip;  

}


推荐阅读