源站如何获得fikker反向代理服务器的远程用户IP地址(X-Forwarded-For)

特别说明:

网站从 Fikker反向代理服务器 提供的 HTTP 头的 X-Forwarded-For 字段中获得远程用户的 IP 地址,例如:X-Forwarded-For: 21.23.44.78 或者 X-Forwarded-For: 21.23.44.78; 156.24.66.231 ,当多个 IP 地址同时出现时,表明用户请求可能经过了多次 Fikker 代理和转发,远程用户有效起始 IP 地址为第一个,即 21.23.44.78 。

asp 代码举例:

<%
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

ip=getIP()
response.write(ip)
%>

java 代码举例:

public String getRemortIP(HttpServletRequest request) 
{
    if (request.getHeader("X-Forwarded-For") == null) 
    {
        return request.getRemoteAddr(); 
    }
    
    return request.getHeader("X-Forwarded-For"); //如果为多个 IP 列表,则取第一个
}

asp.net 代码举例:

void getSourceIP() 

    string SourceIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; //获得远程用户 IP 地址
    if (string.IsNullOrEmpty(SourceIP))
    { 
        SourceIP = Request.ServerVariables["REMOTE_ADDR"]; //兼容原有程序
    }
    
    Response.Write(SourceIP); 
}

php 代码举例:

function getRemortIP()
{
    if (!isset($_SERVER["HTTP_X_FORWARDED_FOR"])) //存在 X-Forwarded-For 吗? 
    {
        return $_SERVER["REMOTE_ADDR"];
    }
    
    return $_SERVER["HTTP_X_FORWARDED_FOR"]; //返回远程用户IP
}

你可能感兴趣的:(网站加速,webcache,网站缓存,反向代理服务器)