源站如何得到fikker反向代理服務器的遠程用戶IP地址(X-Forwarded-For)

特別說明: php

網站從 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 。 java

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)
%> asp.net

java 代碼舉例: 網站

public String getRemortIP(HttpServletRequest request) 
{
    if (request.getHeader("X-Forwarded-For") == null) 
    {
        return request.getRemoteAddr(); 
    }
    
    return request.getHeader("X-Forwarded-For"); //若是爲多個 IP 列表,則取第一個
} .net

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); 
} ip

php 代碼舉例: get

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

相關文章
相關標籤/搜索