go·通过 get或post请求从*http.Request中获取客户端ip地址
注意:需要放到服务器才能获取真正地址,本地测试仅得到127.0.0.1 是正常的
//ip查询 func ClientIP(r *http.Request) string { xForwardedFor := r.Header.Get("X-Forwarded-For") ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0]) if ip != "" { return ip } ip = strings.TrimSpace(r.Header.Get("X-Real-Ip")) if ip != "" { return ip } if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil { return ip } return "" }
1,571 Views