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")
//只是簡(jiǎn)單演示,并不嚴(yán)謹(jǐn)
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>
`)
}
}
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 內(nèi)置了 session 模塊,目前 session 模塊支持的后端引擎包括 memory、cookie、file、mysql、redis、couchbase、memcache、postgres,用戶(hù)也可以根據(jù)相應(yīng)的 interface 實(shí)現(xiàn)自己的引擎。
beego 中使用 session 相當(dāng)方便,只要在 main 入口函數(shù)中設(shè)置如下:

或者通過(guò)配置文件配置如下:

session 有幾個(gè)方便的方法:

session例子:
c.SetSession("loginuser", "adminuser")
fmt.Println("當(dāng)前的session:")
fmt.Println(c.CruSession)
//刪除指定的session
c.DelSession("loginuser")
//銷(xiāo)毀全部的session
c.DestroySession()
islogin=true
fmt.Println("當(dāng)前的session:")
fmt.Println(c.CruSession)
c.SetSession("name", "zhangsan")
//注意:要能c.Ctx.WriteString(name),就要讓它符合輸出的類(lèi)型
name := fmt.Sprintf("%v", c.GetSession("name"))
c.Ctx.WriteString(name)beego.BConfig.WebConfig.Session.SessionOn = true設(shè)置后不能進(jìn)行SetSession等操作,要用上面的方法
創(chuàng)新互聯(lián)www.cdcxhl.cn,專(zhuān)業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買(mǎi)多久送多久。
當(dāng)前文章:beego框架之cookie與session-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article44/pedhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、微信小程序、電子商務(wù)、外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容