beego框架之cookie与session
this.Ctx.SetCookie("name", name, maxage, "/")
创新互联公司-专业网站定制、快速模板网站建设、高性价比城口网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式城口网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖城口地区。费用合理售后完善,十余年实体公司更值得信赖。
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等操作,要用上面的方法
本文题目:beego框架之cookie与session
转载源于:http://www.jxjierui.cn/article/gjhhsd.html


咨询
建站咨询
