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 

 童飞

posted @ 2014-11-03 11:52  天云科技  阅读(5364)  评论(0编辑  收藏  举报