Home > php教程 > php手册 > body text

php获取访问者真实ip地址

WBOY
Release: 2016-06-13 10:00:06
Original
1239 people have browsed it

php获取访问者真实ip地址 在很我的时候我们需要得到用户的真实IP地址,例如,日志记录,地理定位,将用户信息,网站数据分析等,其实获取IP地址很简单$_SERVER['REMOTE_ADDR']就可以了。

php教程获取访问者真实ip地址
在很我的时候我们需要得到用户的真实ip地址,例如,日志记录,地理定位,将用户信息,网站数据分析等,其实获取ip地址很简单$_server['remote_addr']就可以了。
*/
//最简单的方法

$ip = $_server['remote_addr'];

//上面的方法只要使用了代理你就无法得到真实ip地址,下面有更详细的方法

echo "remote addr: " . $_server['remote_addr']."
";
echo "x forward: " . $_server['http_x_forwarded_for']."
";
echo "clien ip: " . $_server['http_client_ip']."
";

//好了来看一个实例。

function getip() {   
 $ip = $_server['remote_addr'];    
 if (!empty($_server['http_client_ip'])) {       
  $ip = $_server['http_client_ip'];   
 } elseif (!empty($_server['http_x_forwarded_for'])) {       
  $ip = $_server['http_x_forwarded_for'];   
 }   
  return $ip;
}

/*
如果是是加密的代理是无法获取真实ip地址的。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template