PHP获取客户端IP地址的方法
1 //获取客户端ip地址
2 private function getRealIp() {
3 if (isset($_SERVER)) {
4 if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
5 $realIp = $_SERVER["HTTP_X_FORWARDED_FOR"];
6 } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
7 $realIp = $_SERVER["HTTP_CLIENT_IP"];
8 } else {
9 $realIp = $_SERVER["REMOTE_ADDR"];
10 }
11 } else {
12 if (getenv("HTTP_X_FORWARDED_FOR")) {
13 $realIp = getenv("HTTP_X_FORWARDED_FOR");
14 } else if (getenv("HTTP_CLIENT_IP")) {
15 $realIp = getenv("HTTP_CLIENT_IP");
16 } else {
17 $realIp = getenv("REMOTE_ADDR");
18 }
19 }
20 return $realIp;
21 }