方法1:
获取客户端公网IP地址PHP
代码如下 | 复制代码 |
/** |
方法2:
获取IP地址PHP
代码如下 | 复制代码 |
function GetRealIP() { static $realip; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } if (strpos($realip, ',') === false) { $sUserIp = $realip; } else { $arrUserIp = explode(',' , $realip); $sUserIp = $arrUserIp[0]; } return $sUserIp; } |
两段代码的效果是一样的,不信大家可以试一下了都非常的好用了。