十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
this.Ctx.SetCookie("name", name, maxage, "/")

this.Ctx.SetCookie("pwd", Md5([]byte(pwd)), maxage, "/")
this.Ctx.GetCookie
cookie例子:
package controllers
import (
"github.com/astaxie/beego"
"log"
)
type TestLoginController struct {
beego.Controller
}
func (c *TestLoginController) Login() {
//获取cookie
name := c.Ctx.GetCookie("name")
password := c.Ctx.GetCookie("password")
//只是简单演示,并不严谨
if name != "" {
c.Ctx.WriteString("Username:" + name + " Password:" + password)
}else{
c.Ctx.WriteString(`
`)
}
}
type the_user struct {
Username string
Password string
}
func (c *TestLoginController) Post() {
u := the_user{}
if err := c.ParseForm(&u); err != nil {
log.Fatal(err)
}
c.Ctx.SetCookie("name", u.Username, 100, "/")
c.Ctx.SetCookie("password", u.Password, 100, "/")
c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password)
}beego 内置了 session 模块,目前 session 模块支持的后端引擎包括 memory、cookie、file、mysql、redis、couchbase、memcache、postgres,用户也可以根据相应的 interface 实现自己的引擎。
beego 中使用 session 相当方便,只要在 main 入口函数中设置如下:

或者通过配置文件配置如下:

session 有几个方便的方法:

session例子:
c.SetSession("loginuser", "adminuser")
fmt.Println("当前的session:")
fmt.Println(c.CruSession)
//删除指定的session
c.DelSession("loginuser")
//销毁全部的session
c.DestroySession()
islogin=true
fmt.Println("当前的session:")
fmt.Println(c.CruSession)
c.SetSession("name", "zhangsan")
//注意:要能c.Ctx.WriteString(name),就要让它符合输出的类型
name := fmt.Sprintf("%v", c.GetSession("name"))
c.Ctx.WriteString(name)beego.BConfig.WebConfig.Session.SessionOn = true设置后不能进行SetSession等操作,要用上面的方法
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。