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

PHP获取用户IP代码实现

Jun 20, 2016 pm 01:04 PM
http ip nbsp quot server

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
  

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


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

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

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

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

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

How to install, uninstall, and reset Windows server backup

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

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

Local IP address query Local IP address query Jan 05, 2024 pm 01:42 PM

Local IP address query

How to cancel your Apple subscription How to cancel your Apple subscription Oct 31, 2023 pm 11:13 PM

How to cancel your Apple subscription

http request 415 error solution http request 415 error solution Nov 14, 2023 am 10:49 AM

http request 415 error solution

See all articles