beego: Index | Files

package captcha

import ""

Package captcha implements generation and verification of image CAPTCHAs. an example for use captcha

“` package controllers

import (



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 {



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">

<input name="captcha" type="text">

</form> “`


Package Files

captcha.go image.go siprng.go

type Captcha Uses

type Captcha struct {

    // url prefix for captcha image
    URLPrefix string

    // specify captcha id input field name
    FieldIDName string
    // specify captcha result input field name
    FieldCaptchaName string

    // captcha image width and height
    StdWidth  int
    StdHeight int

    // captcha chars nums
    ChallengeNums int

    // captcha expiration seconds
    Expiration time.Duration

    // cache key prefix
    CachePrefix string
    // contains filtered or unexported fields

Captcha struct

func NewCaptcha Uses

func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha

NewCaptcha create a new captcha.Captcha

func NewWithFilter Uses

func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha

NewWithFilter create a new captcha.Captcha and auto AddFilter for serve captacha image and add a template func for output html

func (*Captcha) CreateCaptcha Uses

func (c *Captcha) CreateCaptcha() (string, error)

CreateCaptcha create a new captcha id

func (*Captcha) CreateCaptchaHTML Uses

func (c *Captcha) CreateCaptchaHTML() template.HTML

CreateCaptchaHTML template func for output html

func (*Captcha) Handler Uses

func (c *Captcha) Handler(ctx *context.Context)

Handler beego filter handler for serve captcha image

func (*Captcha) Verify Uses

func (c *Captcha) Verify(id string, challenge string) (success bool)

Verify direct verify id and challenge string

func (*Captcha) VerifyReq Uses

func (c *Captcha) VerifyReq(req *http.Request) bool

VerifyReq verify from a request

type Image Uses

type Image struct {
    // contains filtered or unexported fields

Image struct

func NewImage Uses

func NewImage(digits []byte, width, height int) *Image

NewImage returns a new captcha image of the given width and height with the given digits, where each digit must be in range 0-9.

func (*Image) WriteTo Uses

func (m *Image) WriteTo(w io.Writer) (int64, error)

WriteTo writes captcha image in PNG format into the given writer.

Package captcha imports 20 packages (graph) and is imported by 32 packages. Updated 2017-04-25. Refresh now. Tools for package owners.