iris获取引用者(extract referer)

    main.go

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