获取客户端IP地址
Jun 07, 2016 am 11:43 AM发现get_client_ip在局域网代理模式下面部分机器无法获取IP,故修改get_client_ip如下解决,但也有可能是我们这边服务器配置的问题:
修改位置:ThinkPHP\Common\funcitons.php->get_client_ip()function get_client_ip($type = 0) {<br>
$type = $type ? 1 : 0;<br>
static $ip = NULL;<br>
if ($ip !== NULL) return $ip[$type];<br>
if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下,获取客户端真实IP<br>
$ip=$_SERVER['HTTP_X_REAL_IP']; <br>
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip<br>
$ip = $_SERVER['HTTP_CLIENT_IP'];<br>
}elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关<br>
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);<br>
$pos = array_search('unknown',$arr);<br>
if(false !== $pos) unset($arr[$pos]);<br>
$ip = trim($arr[0]);<br>
}elseif (isset($_SERVER['REMOTE_ADDR'])) {<br>
$ip = $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址<br>
}else{<br>
$ip=$_SERVER['REMOTE_ADDR'];<br>
}<br>
// IP地址合法验证<br>
$long = sprintf("%u",ip2long($ip));<br>
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);<br>
return $ip[$type];<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元

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

Learn about introductory code examples for Python programming

PHP variables in action: 10 real-life examples of use

From beginner to proficient: Code implementation of commonly used data structures in Go language

How to use PHP to write inventory management function code in the inventory management system

Java implements simple bubble sort code

Go language programming examples: code examples in web development

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces

Guidance and Examples: Learn to implement the selection sort algorithm in Java
