Home  >  Article  >  Backend Development  >  How to get user IP address in PHP

How to get user IP address in PHP

不言
不言Original
2018-04-03 11:04:061993browse

This article introduces the method of obtaining the user's IP address in PHP. Now I share it with you and give a reference to friends who need help. Let's take a look together

/** 
* 用来获取用户的ip地址方法 
* @return array|false|string 用户的ip地址 
*/ 
   public function getUserIP() 
   { 
       if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknow")){ 
           $ip = getenv("HTTP_CLIENT_IP"); 
       }else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknow")){ 
           $ip = getenv("HTTP_X_FORWARDED_FOR"); 
       }else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknow")){ 
           $ip = getenv("REMOTE_ADDR"); 
       }else if(isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"],"unknow")){ 
           $ip = $_SERVER["REMOTE_ADDR"]; 
       }else{ 
           $ip = "unknow"; 
       } 
       return $ip; 
   }

Related recommendations:

<a href="http://www.php.cn/php-weizijiaocheng-391095.html" target="_self">Detailed explanation of php getting the file size</a>

PHP method to get the current domain name

php method to get start time and end time

The above is the detailed content of How to get user IP address in PHP. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:PHP&&& Array - CSDN BlogNext article:PHP&&& Array - CSDN Blog