使用coldfusion获取用户的真实ip地址

Pat*_*urg 5 coldfusion

我发现与用户 IP 相关的唯一变量如下:

<cfif #CGI.HTTP_X_Forwarded_For# EQ "">
                <CFSET ipaddress="#CGI.Remote_Addr#">
            <cfelse>
                <CFSET ipaddress="#CGI.HTTP_X_Forwarded_For#">
            </cfif>
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以检查coldfusion中的真实IP地址吗?

Kev*_*ris 5

您拥有的代码已经在寻找“最著名的”客户端 IP 地址方面做得不错。这是我在项目中使用的过度设计的代码:

public string function getClientIp() {
    local.response = "";

    try {
        try {
            local.headers = getHttpRequestData().headers;
            if (structKeyExists(local.headers, "X-Forwarded-For") && len(local.headers["X-Forwarded-For"]) > 0) {
                local.response = trim(listFirst(local.headers["X-Forwarded-For"]));
            }
        } catch (any e) {}

        if (len(local.response) == 0) {
            if (structKeyExists(cgi, "remote_addr") && len(cgi.remote_addr) > 0) {
                local.response = cgi.remote_addr;
            } else if (structKeyExists(cgi, "remote_host") && len(cgi.remote_host) > 0) {
                local.response = cgi.remote_host;
            }
        }
    } catch (any e) {}

    return local.response;
}
Run Code Online (Sandbox Code Playgroud)