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

时间:2016-08-18 11:59:22

标签: coldfusion

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

<cfif #CGI.HTTP_X_Forwarded_For# EQ "">
                <CFSET ipaddress="#CGI.Remote_Addr#">
            <cfelse>
                <CFSET ipaddress="#CGI.HTTP_X_Forwarded_For#">
            </cfif>

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

1 个答案:

答案 0 :(得分:2)

您所拥有的代码已经在寻找“最知名”的客户端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;
}
相关问题