PHP获取用户IP代码实现
Jun 20, 2016 pm 01:04 PMPHP获取用户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 /> }
这边只是提供参考,大家要是有什么好的方法可以留言交流!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari?

Win10/11 digital activation script MAS version 2.2 re-supports digital activation

How to install, uninstall, and reset Windows server backup

Understand common application scenarios of web page redirection and understand the HTTP 301 status code

How to cancel your Apple subscription
