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.

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致