package controllers
import (
"fmt"
"github.com/astaxie/beego"
)
type Test_loginController struct{
beego.Controller
}
func (c *Test_loginController) Login(){
username1:=c.GetSession("username")
password1:=c.GetSession("password")
//接口转string
fmt.Println(username1)
fmt.Println(password1)
if username1!=nil{
c.Ctx.WriteString("<html>session:")
c.Ctx.WriteString("username:"+username1.(string)+" ")
c.Ctx.WriteString("password:"+password1.(string)+"</html>")
}
name:=c.Ctx.GetCookie("username")
password:=c.Ctx.GetCookie("password")
//清除cookie
/*c.Ctx.SetCookie("username",name,-1)
c.Ctx.SetCookie("password",password,-1)*/
if name!=""{
c.Ctx.WriteString("<br><html>cookie:Username:"+name+" Password:"+password+"</html>")
}else{
c.Ctx.WriteString(`<html><form action="http://127.0.0.1:8080/test_login" method="post">用户名:<input type="text" name="Username"/><br>密码:<input type="password" name="Password"/><br><input type="submit" value="登录"/></form></html>`)
}
}
func (c *Test_loginController) Post(){
user:=User{}
err:=c.ParseForm(&user)
if err!=nil{
}else{
//设置session
c.SetSession("username",user.Username)
c.SetSession("password",user.Password)
//设置cookie
c.Ctx.SetCookie("username",user.Username,100)
c.Ctx.SetCookie("password",user.Password,100)
c.Ctx.WriteString("Username:"+user.Username+"\n")
c.Ctx.WriteString("Password:"+user.Password+"\n")
}
}
beego中session和cookie使用
最新推荐文章于 2024-08-11 22:57:02 发布