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 

 童飞


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM