Home > Backend Development > PHP Tutorial > PHP获取用户IP代码实现

PHP获取用户IP代码实现

WBOY
Release: 2016-06-20 13:04:28
Original
1051 people have browsed it

PHP获取用户IP代码实现

获取客户端用户IP在网站中经常会需要,譬如:    

1.登陆IP:可以及时知道自己的账号是不是被盗用;    

2.投票:可以针对每个IP只能做一次投票防止刷票;    

3.统计:统计用户来源,这个是不可或缺的数据分析部分;    

4.其他。   

 所以正确获取客户端用户IP还是很重要的,下面是日常使用中获取IP的PHP代码:

/**<br />		 * 获取客户IP地址<br />		 * @return $ip<br />		 */<br />	    function getIp(){<br />	    	if(!empty($_SERVER["HTTP_CLIENT_IP"])){<br />			  $ip = $_SERVER["HTTP_CLIENT_IP"];<br />			}<br />			elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){<br />			  $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];<br />			}<br />			elseif(!empty($_SERVER["REMOTE_ADDR"])){<br />			  $ip = $_SERVER["REMOTE_ADDR"];<br />			}<br />			else{<br />			  $ip = "127.0.0.1";<br />			}<br />			return $ip;<br />	    }   
Copy after login
  

这边只是提供参考,大家要是有什么好的方法可以留言交流!


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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template