增加记录IP

This commit is contained in:
hoteas
2022-08-20 22:38:15 +08:00
parent cf29db5d5c
commit febd8d9805
+11
View File
@@ -5,6 +5,7 @@ import (
. "code.hoteas.com/golang/hotime/db"
"encoding/json"
"net/http"
"strings"
"time"
)
@@ -74,6 +75,16 @@ func (that *Context) View() {
if that.Session("user_id").Data != nil {
that.Log["user_id"] = that.Session("user_id").ToCeilInt()
}
ipStr := Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":"))
//负载均衡优化
if ipStr == "127.0.0.1" {
if that.Req.Header.Get("X-Forwarded-For") != "" {
ipStr = that.Req.Header.Get("X-Forwarded-For")
} else if that.Req.Header.Get("X-Real-IP") != "" {
ipStr = that.Req.Header.Get("X-Real-IP")
}
}
that.Log["ip"] = ipStr
that.Db.Insert("logs", that.Log)
}