Home > Article > Backend Development > How to get user IP address in PHP
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!