各种语言获取客户端真是ip的方法

1. PHP脚本

整体思路:

需要依次检查HTTP_CLIENT_IP、HTTP_X_FORWARDED_FOR、REMOTE_ADDR这三个是否存在,只要一旦有一个存在就结束

获取示例代码:

function GetIP(){

if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))

$ip = getenv("HTTP_CLIENT_IP");

else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))

$ip = getenv("HTTP_X_FORWARDED_FOR");

else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))

$ip = getenv("REMOTE_ADDR");

else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))

$ip = $_SERVER['REMOTE_ADDR'];

else

$ip = "unknown";

return($ip);

}

其中对获取ip的三个属性HTTP_VIA、HTTP_X_FORWARDED_FOR、REMOTE_ADDR在使用代理服务器时获取的情况

一、没有使用代理服务器的情况

REMOTE_ADDR = 自己的真实IP

HTTP_VIA = 没数值或不显示

HTTP_X_FORWARDED_FOR = 没数值或不显示

 

二、使用Transparent Proxies(透明代理)服务器的情况

REMOTE_ADDR = 最后一个代理服务器IP

HTTP_VIA = 代理服务器IP

HTTP_X_FORWARDED_FOR = 自己的真实IP

 

三、使用Anonymous Proxies(匿名代理)服务器的情况

REMOTE_ADDR = 最后一个代理服务器IP

HTTP_VIA = 代理服务器IP

HTTP_X_FORWARDED_FOR = 代理服务器IP

 

四、使用Distorting Proxies(欺骗代理)服务器的情况

REMOTE_ADDR = 代理服务器IP

HTTP_VIA = 代理服务器IP

HTTP_X_FORWARDED_FOR = 随机IP

经过多个代理服务器时,HTTP_X_FORWARDED_FOR值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215中的之一

 

五、使用Elite Proxies服务器的情况

REMOTE_ADDR = 代理服务器IP

HTTP_VIA = 没有数值或不显示

HTTP_X_FORWARDED_FOR = 没有数值或不显示

综上来看,PHP获取客户端真实IP的方法并不是能够百分之百成功的。

 

2. ASP脚本

获取思路:

和PHP脚本的类似,也是通过判断HTTP_X_FORWARDED_FOR、REMOTE_ADDR来获取的。但是由于客户端使用代理服务器缘故,并不能保证100%能够获取客户端真实ip

获取示例代码:

Private Function getIP()

  Dim strIPAddr

  If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = "" OR InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), "unknown") > 0 Then

  strIPAddr = Request.ServerVariables("REMOTE_ADDR")

  ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") > 0 Then

  strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",")-1)

  ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") > 0 Then

  strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";")-1)

  Else

  strIPAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

  End If

  getIP = Trim(Mid(strIPAddr, 1, 30))

  End Function

 

3. C# 方法

获取思路,和上面两种脚本函数的思路类似,获取示例代码如下:

public static string GetIPAddress()

   {

       string user_IP = string.Empty;

       if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)

       {

           if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)

           {

               user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

           }

           else

           {

               user_IP = System.Web.HttpContext.Current.Request.UserHostAddress;

           }

       }

       else

       {

           user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

       }

       return user_IP;

 

4. Java 方法

package com.viathink.utils;       //需要的Java包

import javax.servlet.http.HttpServletRequest ;   //导入方法

public static String getIpAddr(HttpServletRequest request) {

String ip = request.getHeader("X-Forwarded-For");

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("Proxy-Client-IP");

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("WL-Proxy-Client-IP");

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("HTTP_CLIENT_IP");

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("HTTP_X_FORWARDED_FOR");

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getRemoteAddr();

}

return ip;

}

}

 

5. Python 脚本

获取示例代码:

def get_client_ip(request):

try:

   real_ip = request.META['HTTP_X_FORWARDED_FOR']

   regip = real_ip.split(",")[0]

except:

   try:

       regip = request.META['REMOTE_ADDR']

   except:

       regip = ""

return regip