beego簡單的驗證碼實現以及驗證

 

/** * 程序 */
package controllers

import (
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/cache"
    "github.com/astaxie/beego/utils/captcha"
)

type LoginController struct {
    beego.Controller
}

var cpt *captcha.Captcha

func init() {
    // use beego cache system store the captcha data
    store := cache.NewMemoryCache()
    cpt = captcha.NewWithFilter("/captcha/", store)
}

func (this *LoginController) Index() {

    if this.Ctx.Input.Method() == "POST" {
     // 驗證輸入
        this.Data["Success"] = cpt.VerifyReq(this.Ctx.Request)
        this.TplNames = "login.html"
    } else {
        this.TplNames = "login.html"
    }

}

/** * 視圖 login.html */
{{.Success}}
   <form method="post" action="/">
    <input type="text" name="captcha" />
        {{create_captcha}}<br />
    <input type="submit" value="提交" />
  </form>

附帶第三方包:
go get github.com/astaxie/beego/cache
 
go get github.com/astaxie/beego/utils/captcha

 童飛html

相關文章
相關標籤/搜索