擷取用戶端真實IP地址,擷取用戶端ip
1、需要引入log4j-1.2.14.jar
package org.ydd.test;import java.util.Enumeration;import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;/** * @author coco * @version * 擷取IP4 */public class IP4 {private static final Logger log = Logger.getLogger(IP4.class);/** * 擷取用戶端真實IP地址(注意:如果通過Squid反向 Proxy軟體,需更改squid.conf設定檔forwarded_for項預設是為on, * 否則擷取到的IP為unknown) * @param request * @return */@SuppressWarnings({ "rawtypes", "unchecked" })public static String getIpAddr(HttpServletRequest request) {log.debug("====================================================");if(log.isDebugEnabled()){StringBuffer buf = new StringBuffer("all head info:\n");Enumeration enumeration = request.getHeaderNames();while(enumeration.hasMoreElements()){Object head = enumeration.nextElement();if(null != head){String value = request.getHeader(String.valueOf(head));buf.append(head + "=" + value + "\n");}}log.debug(buf.toString());}String ip = request.getHeader("x-forwarded-for");log.debug("request.getHeader(\"x-forwarded-for\")=" + ip);if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("X-Forwarded-For");log.debug("request.getHeader(\"X-Forwarded-For\")=" + ip);}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");log.debug("request.getHeader(\"Proxy-Client-IP\")=" + ip);}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");log.debug("request.getHeader(\"WL-Proxy-Client-IP\")=" + ip);}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("HTTP_CLIENT_IP");log.debug("request.getHeader(\"HTTP_CLIENT_IP\")=" + ip);}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("HTTP_X_FORWARDED_FOR");log.debug("request.getHeader(\"HTTP_X_FORWARDED_FOR\")=" + ip);}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();log.debug("request.getRemoteAddr()=" + ip);}if(null != ip && ip.indexOf(',') != -1){//如果通過了多級反向 Proxy的話,X-Forwarded-For的值並不止一個,而是一串 IP 值//取X-Forwarded-For中第一個非unknown的有效IP字串//如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100//使用者真實IP為: 192.168.1.110 //注意:當訪問地址為 localhost 時 地址格式為 0:0:0:0:0:0:1log.debug("ip=" + ip);String[] ips = ip.split(",");for (int i = 0; i < ips.length; i++) {if(null != ips[i] && !"unknown".equalsIgnoreCase(ips[i])){ip = ips[i];break;}}if("0:0:0:0:0:0:1".equals(ip)){log.warn("由於用戶端訪問地址使用 localhost,擷取用戶端真實IP地址錯誤,請使用IP方式訪問");}}if("unknown".equalsIgnoreCase(ip)){log.warn("由於用戶端通過Squid反向 Proxy軟體訪問,擷取用戶端真實IP地址錯誤,請更改squid.conf設定檔forwarded_for項預設是為on解決");}log.debug("====================================================");return ip;}}
ASP擷取用戶端真實IP??
在ASP中使用 Request.ServerVariables("REMOTE_ADDR") 來取得用戶端的IP地址,但如果用戶端是使用Proxy 伺服器來訪問,那取到的就是Proxy 伺服器的IP地址,而不是真正的用戶端IP地址。要想透過Proxy 伺服器取得用戶端的真實IP地址,就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 來讀取。不過要注意的事,並不是每個Proxy 伺服器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 來讀取用戶端的真實 IP,有些用此方法讀取到的仍然是Proxy 伺服器的IP。還有一點需要注意的是:如果用戶端沒有通過Proxy 伺服器來訪問,那麼用 Request.ServerVariables ("HTTP_X_FORWARDED_FOR") 取到的值將是空的。因此,如果要在程式中使用此方法,可以這樣處理:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......
即:如果用戶端通過Proxy 伺服器,則取 HTTP_X_FORWARDED_FOR 的值,如果沒通過Proxy 伺服器,就取 REMOTE_ADDR 的值。
'通用函數:如果不能取用戶端真實IP,就會取用戶端的代理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_FORW......餘下全文>>
擷取用戶端IP地址
如果你測試訪問的時候,使用的是 localhost 進行訪問的,那麼伺服器端獲得的也就是127.0.0.1,因為localhost是一個環路地址,特殊的.
如果你是通過你的ip地址進行訪問的,那麼獲得的IP地址就會是正確的了.
-------
要通過自己本地的IP進行訪問,在控制台的網路連接裡找到本地串連,查看它的屬性,並找到"詳細資料"按鈕,點擊後彈出的對話方塊中IPV4地址就是本地地址.
或開啟命令列,輸入 ipconfig /all 即可找到本地地址.
在瀏覽器中通過這個IP地址訪問,就不會是127.0.0.1了.