Macaron Documentation
  • Welcome
  • Starter Guide
  • Core Concepts
  • Core Services
  • Custom Services
  • Middlewares
    • Routing
    • Templating
    • Gzip
    • Localization
    • Data Binding and Validation
    • Cache
    • Captcha
    • Session
    • Cross-Site Request Forgery
    • Embed Binary Data
    • Serving Multiple Sites
  • FAQs
  • 简体中文
    • 初学者指南
    • 核心概念
    • 核心服务
    • 自定义服务
    • 中间件和辅助模块
      • 路由模块
      • 模板引擎
      • Gzip 压缩
      • 应用本地化
      • 数据绑定与验证
      • 缓存管理(Cache)
      • 验证码服务
      • 会话管理(Session)
      • 跨域请求攻击(CSRF)
      • 嵌入二进制数据
      • 服务多个站点
    • 常见问题
Powered by GitBook
On this page
  • Installation
  • Usage
  • Options

Was this helpful?

  1. Middlewares

Captcha

PreviousCacheNextSession

Last updated 5 years ago

Was this helpful?

Middleware captcha provides captcha service for Macaron .

Installation

go get github.com/go-macaron/captcha

Usage

To use this middleware, you have to register first.

// main.go
import (
    "github.com/go-macaron/cache"
    "github.com/go-macaron/captcha"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.Classic()
    m.Use(cache.Cacher())
    m.Use(captcha.Captchaer())

    m.Get("/", func(ctx *macaron.Context, cpt *captcha.Captcha) string {
        if cpt.VerifyReq(ctx.Req) {
            return "valid captcha"
        }
        return "invalid captcha"
    })

    m.Run()
}
<!-- templates/hello.tmpl -->
{{.Captcha.CreateHtml}}

Options

captcha.Captchaer comes with a variety of configuration options:

// ...
m.Use(captcha.Captchaer(captcha.Options{
    // URL prefix of getting captcha pictures. Default is "/captcha/".
    URLPrefix:            "/captcha/",
    // Hidden input element ID. Default is "captcha_id".
    FieldIdName:        "captcha_id",
    // User input value element name in request form. Default is "captcha".
    FieldCaptchaName:    "captcha",
    // Challenge number. Default is 6.
    ChallengeNums:        6,
    // Captcha image width. Default is 240.
    Width:                240,
    // Captcha image height. Default is 80.
    Height:                80,
    // Captcha expiration time in seconds. Default is 600.
    Expiration:            600,
    // Cache key prefix captcha characters. Default is "captcha_".
    CachePrefix:        "captcha_",
}))
// ...
GitHub
API Reference
cache
Instances