iris获取引用者(extract referer)

目录结构

主目录extractReferer

  1. —— main.go

代码示例

main.go

  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. "github.com/kataras/iris/context"
  5. )
  6. func main() {
  7. app := iris.New()
  8. app.Get("/", func(ctx context.Context) /*或iris.Context,Go 1.9+也是如此*/ {
  9. // GetReferrer提取并返回指定的Referer标头中的信息
  10. //在https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy中或通过URL查询参数是referer。
  11. r := ctx.GetReferrer()
  12. switch r.Type {
  13. case context.ReferrerSearch:
  14. ctx.Writef("Search %s: %s\n", r.Label, r.Query)
  15. ctx.Writef("Google: %s\n", r.GoogleType)
  16. case context.ReferrerSocial:
  17. ctx.Writef("Social %s\n", r.Label)
  18. case context.ReferrerIndirect:
  19. ctx.Writef("Indirect: %s\n", r.URL)
  20. }
  21. })
  22. //URL查询参数是referer
  23. // http://localhost:8080?referer=https://twitter.com/Xinterio/status/1023566830974251008
  24. // http://localhost:8080?referer=https://www.google.com/search?q=Top+6+golang+web+frameworks&oq=Top+6+golang+web+frameworks
  25. app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
  26. }

提示

  • 查询网站页面跳转referrer信息