Home  >  Article  >  Backend Development  >  PHP获取当前用户真实IP的方法

PHP获取当前用户真实IP的方法

WBOY
WBOYOriginal
2016-06-20 13:02:48993browse

近日做个项目跟Discuz有关,发现里面有很多东西写的非常不错呢,于是乎想着将里面的一些比较实用的功能提取出来和大家分享一下!毕竟Discuz里面的东东应该还算是非常不错的,比网上东找西找的那些信息要靠谱些吧!下面就分享一个利用PHP获取当前用户真实的IP地址的方法。

<p>function getIp(){</p>	$onlineip='';<br />	if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')){<br />		$onlineip=getenv('HTTP_CLIENT_IP');<br />	} elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown')){<br />		$onlineip=getenv('HTTP_X_FORWARDED_FOR');<br />	} elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown')){<br />		$onlineip=getenv('REMOTE_ADDR');<br />	} elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){<br />		$onlineip=$_SERVER['REMOTE_ADDR'];<br />	}<br />	return $onlineip;<br /><p>}



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