1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 22:11:29 +00:00

fix set cookies many times

This commit is contained in:
astaxie 2013-05-07 15:36:43 +08:00
parent 18bf474951
commit 6af2778ab9
2 changed files with 29 additions and 20 deletions

View File

@ -66,6 +66,6 @@ func (ctx *Context) SetCookie(name string, value string, age int64) {
} else { } else {
utctime = time.Unix(time.Now().Unix()+age, 0) utctime = time.Unix(time.Now().Unix()+age, 0)
} }
cookie := fmt.Sprintf("%s=%s; expires=%s", name, value, webTime(utctime)) cookie := fmt.Sprintf("%s=%s; Expires=%s; Path=/", name, value, webTime(utctime))
ctx.SetHeader("Set-Cookie", cookie, false) ctx.SetHeader("Set-Cookie", cookie, true)
} }

View File

@ -21,12 +21,13 @@ import (
) )
type Controller struct { type Controller struct {
Ctx *Context Ctx *Context
Data map[interface{}]interface{} Data map[interface{}]interface{}
ChildName string ChildName string
TplNames string TplNames string
Layout string Layout string
TplExt string TplExt string
CruSession session.SessionStore
} }
type ControllerInterface interface { type ControllerInterface interface {
@ -58,6 +59,9 @@ func (c *Controller) Prepare() {
} }
func (c *Controller) Finish() { func (c *Controller) Finish() {
if c.CruSession != nil {
c.CruSession.SessionRelease()
}
} }
func (c *Controller) Get() { func (c *Controller) Get() {
@ -259,25 +263,30 @@ func (c *Controller) SaveToFile(fromfile, tofile string) error {
return nil return nil
} }
func (c *Controller) StartSession() (sess session.SessionStore) { func (c *Controller) StartSession() session.SessionStore {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) if c.CruSession == nil {
return c.CruSession = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
}
return c.CruSession
} }
func (c *Controller) SetSession(name string, value interface{}) { func (c *Controller) SetSession(name string, value interface{}) {
ss := c.StartSession() if c.CruSession == nil {
defer ss.SessionRelease() c.StartSession()
ss.Set(name, value) }
c.CruSession.Set(name, value)
} }
func (c *Controller) GetSession(name string) interface{} { func (c *Controller) GetSession(name string) interface{} {
ss := c.StartSession() if c.CruSession == nil {
defer ss.SessionRelease() c.StartSession()
return ss.Get(name) }
return c.CruSession.Get(name)
} }
func (c *Controller) DelSession(name string) { func (c *Controller) DelSession(name string) {
ss := c.StartSession() if c.CruSession == nil {
defer ss.SessionRelease() c.StartSession()
ss.Delete(name) }
c.CruSession.Delete(name)
} }