Captcha
Middleware captcha provides captcha service for Macaron Instances.

Installation

1
go get github.com/go-macaron/captcha
Copied!

Usage

To use this middleware, you have to register cache first.
1
// main.go
2
import (
3
"github.com/go-macaron/cache"
4
"github.com/go-macaron/captcha"
5
"gopkg.in/macaron.v1"
6
)
7
8
func main() {
9
m := macaron.Classic()
10
m.Use(cache.Cacher())
11
m.Use(captcha.Captchaer())
12
13
m.Get("/", func(ctx *macaron.Context, cpt *captcha.Captcha) string {
14
if cpt.VerifyReq(ctx.Req) {
15
return "valid captcha"
16
}
17
return "invalid captcha"
18
})
19
20
m.Run()
21
}
Copied!
1
<!-- templates/hello.tmpl -->
2
{{.Captcha.CreateHtml}}
Copied!

Options

captcha.Captchaer comes with a variety of configuration options:
1
// ...
2
m.Use(captcha.Captchaer(captcha.Options{
3
// URL prefix of getting captcha pictures. Default is "/captcha/".
4
URLPrefix: "/captcha/",
5
// Hidden input element ID. Default is "captcha_id".
6
FieldIdName: "captcha_id",
7
// User input value element name in request form. Default is "captcha".
8
FieldCaptchaName: "captcha",
9
// Challenge number. Default is 6.
10
ChallengeNums: 6,
11
// Captcha image width. Default is 240.
12
Width: 240,
13
// Captcha image height. Default is 80.
14
Height: 80,
15
// Captcha expiration time in seconds. Default is 600.
16
Expiration: 600,
17
// Cache key prefix captcha characters. Default is "captcha_".
18
CachePrefix: "captcha_",
19
}))
20
// ...
Copied!
Last modified 2yr ago
Copy link