dongyo1959 2015-08-11 21:12
浏览 44
已采纳

大猩猩会议套餐混乱

Coming from a PHP background I'm a little confuse about Gorilla sessions package.

Does Gorilla act similar to $_SESSION['name'] or does it act similar to $_COOKIE['name'] from PHP?

I'm trying to use both ways to create a user session for my Go web application, but I'm not sure if Gorilla sessions will be a good package to use. I want the users who didn't click on the "remember me" button on the login form to have their session erased after closing their browser, whereas everyone else will have a cookie associated with them. So would Gorilla sessions be able to handle both scenarios or should I use something else in this case?

  • 写回答

1条回答 默认 最新

  • drblhw5731 2015-08-12 02:02
    关注

    It entirely depends on which storage back-end you use.

    The gorilla/sessions package has built-in cookie & filesystem based stores. There is no memory-based store, which is roughly what PHP's $_SESSION is.

    My recommendation:

    • Use the built-in cookie store, which uses signed cookies. It is well suited for most purposes and is the easiest to implement.
    • If you have a need for server-side sessions (i.e. storing large values in the session), pick from the available implementations - Redis, BoltDB, mySQL, Postgres, etc.

    I have first-hand experience with the Redis backed store (redistore), which has been great. The BoltDB (a file-based key store) and Postgres stores are also solid if you have a preference for those.

    I want the users who didn't click on the "remember me" button on the login form to have their session erased after closing their browser, whereas everyone else will have a cookie associated with them. So would Gorilla sessions be able to handle both scenarios or should I use something else in this case?

    Note that all implementations require a "cookie" - it's just whether the cookie is the self-contained store, or whether it just holds an identifier referring to a row/value in the back-end store.

    You can set "session cookies" (i.e. last only as long as the tab/browser session) by setting session.Options.MaxAge = 0 as per this part of the gorilla/sessions docs.

    e.g.

    func MyHandler(w http.ResponseWriter, r *http.Request) {
        session, err := store.Get(r, "session-name")
        if err != nil {
            http.Error(w, err.Error(), 500)
            return
        }
    
        // Add your logic to check the r.FormValue for your remember_me checkbox.
    
        // Temporary session
        session.Options.MaxAge = 0
    
        // Set some session values.
        session.Values["user"] = someUser
        // Save it before we write to the response/return from the handler.
        session.Save(r, w)
    }
    

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误