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

How to get user IP address in PHP

不言
Release: 2023-03-23 06:34:01
Original
2001 people have browsed it

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; 
   }
Copy after login

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!

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