1、cookie的用法
this.Ctx.SetCookie("name", name, maxage, "/")
this.Ctx.SetCookie("pwd", Md5([]byte(pwd)), maxage, "/") this.Ctx.GetCookie
2、session控制
beego 内置了 session 模块,目前 session 模块支持的后端引擎包括 memory、cookie、file、mysql、redis、couchbase、memcache、postgres,用户也可以根据相应的 interface 实现自己的引擎。
beego 中使用 session 相当方便,只要在 main 入口函数中设置如下:
beego.BConfig.WebConfig.Session.SessionOn = true
或者通过配置文件配置如下:
sessionon = true
session 有几个方便的方法:
- SetSession(name string, value interface{})
- GetSession(name string) interface{}
- DelSession(name string)
- SessionRegenerateID()
- DestroySession()
session 操作主要有设置 session、获取 session、删除 session。
3、cookie与session用法
示例:
routers/router.go
package routers import (
"web/controllers"
"github.com/astaxie/beego"
) func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/test_input", &controllers.TestInputController{}, "get:Get;post:Post")
beego.Router("/test_login", &controllers.TestLoginController{}, "get:Login;post:Post")
}
verws/main.go
package main import (
_ "web/routers"
"github.com/astaxie/beego"
) func main() {
beego.BConfig.WebConfig.Session.SessionOn = true
beego.Run()
}
controllers/testInput.go
package controllers import (
"github.com/astaxie/beego"
) type TestInputController struct {
beego.Controller
} type User struct{
Username string
Password string
} func (c *TestInputController) Get(){
//id := c.GetString("id")
//c.Ctx.WriteString("<html>" + id + "<br/>") //name := c.Input().Get("name")
//c.Ctx.WriteString(name + "</html>")
name := c.GetSession("name")
password := c.GetSession("password") if nameString, ok := name.(string); ok && nameString != ""{
c.Ctx.WriteString("Name:" + name.(string) + " password:" + password.(string))
}else{
c.Ctx.WriteString(`<html><form action="http://127.0.0.1:8080/test_input" method="post">
<input type="text" name="Username"/>
<input type="password" name="Password"/>
<input type="submit" value="提交"/>
</form></html>`)
}
} func (c *TestInputController) Post(){
u := User{}
if err:=c.ParseForm(&u) ; err != nil{
//process error
} c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password)
}
controllers/testLogin.go
package controllers import (
"github.com/astaxie/beego"
) type TestLoginController struct {
beego.Controller
} type UserInfoV2 struct{
Username string
Password string
} func (c *TestLoginController) Login(){
name := c.Ctx.GetCookie("name")
password := c.Ctx.GetCookie("password") //do verify work
if name != ""{
c.Ctx.WriteString("Username:" + name + " Password:" + password)
}else{
c.Ctx.WriteString(`<html><form action="http://127.0.0.1:8080/test_login" method="post">
<input type="text" name="Username"/>
<input type="password" name="Password"/>
<input type="submit" value="提交"/>
</form></html>`)
}
} func (c *TestLoginController) Post(){
u := UserInfoV2{}
if err:=c.ParseForm(&u) ; err != nil{
//process error
} c.Ctx.SetCookie("name", u.Username, 100, "/")
c.Ctx.SetCookie("password", u.Password, 100, "/")
c.SetSession("name", u.Username)
c.SetSession("password", u.Password)
c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password)
}