46 lines
		
	
	
		
			728 B
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			728 B
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| # Captcha
 | |
| 
 | |
| an example for use captcha
 | |
| 
 | |
| ```
 | |
| package controllers
 | |
| 
 | |
| import (
 | |
| 	"github.com/beego/beego"
 | |
| 	"github.com/beego/beego/cache"
 | |
| 	"github.com/beego/beego/utils/captcha"
 | |
| )
 | |
| 
 | |
| var cpt *captcha.Captcha
 | |
| 
 | |
| func init() {
 | |
| 	// use beego cache system store the captcha data
 | |
| 	store := cache.NewMemoryCache()
 | |
| 	cpt = captcha.NewWithFilter("/captcha/", store)
 | |
| }
 | |
| 
 | |
| type MainController struct {
 | |
| 	beego.Controller
 | |
| }
 | |
| 
 | |
| func (this *MainController) Get() {
 | |
| 	this.TplName = "index.tpl"
 | |
| }
 | |
| 
 | |
| func (this *MainController) Post() {
 | |
| 	this.TplName = "index.tpl"
 | |
| 
 | |
| 	this.Data["Success"] = cpt.VerifyReq(this.Ctx.Request)
 | |
| }
 | |
| ```
 | |
| 
 | |
| template usage
 | |
| 
 | |
| ```
 | |
| {{.Success}}
 | |
| <form action="/" method="post">
 | |
| 	{{create_captcha}}
 | |
| 	<input name="captcha" type="text">
 | |
| </form>
 | |
| ```
 |