Home  >  Article  >  php教程  >  如何取得用户的真实IP

如何取得用户的真实IP

WBOY
WBOYOriginal
2016-06-21 09:05:12931browse
如何取得用户的真实IP


PHP代码:--------------------------------------------------------------------------------


function iptype1 () {
if (getenv("HTTP_CLIENT_IP")) {
  return getenv("HTTP_CLIENT_IP");
}
else {
  return "none";
}
}
function iptype2 () {
if (getenv("HTTP_X_FORWARDED_FOR")) {
  return getenv("HTTP_X_FORWARDED_FOR");
}
else {
  return "none";
}
}
function iptype3 () {
if (getenv("REMOTE_ADDR")) {
  return getenv("REMOTE_ADDR");
}
else {
  return "none";
}
}
function ip() {
$ip1 = iptype1();
$ip2 = iptype2();
$ip3 = iptype3();
if (isset($ip1) && $ip1 != "none" && $ip1 != "unknown") {
  return $ip1;
}
elseif (isset($ip2) && $ip2 != "none" && $ip2 != "unknown") {
  return $ip2;
}
elseif (isset($ip3) && $ip3 != "none" && $ip3 != "unknown") {
  return $ip3;
}   
else {
return "none";
}
}

Echo ip();
?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn