验证码

验证码分成两部分:一个是展示验证码的页面;一个是验证验证码。

于是我们可以先定义两个接口:

  1. // Get - address: http://127.0.0.1:8080/
  2. func (ctrl *Controller) Get() {
  3. ctrl.TplName = "captcha.html"
  4. ctrl.Data["name"] = "Home"
  5. // don't forget this
  6. _ = ctrl.Render()
  7. }
  8. // Captcha - address: http://127.0.0.1:8080/sendCaptcha
  9. func (ctrl *Controller) Captcha() {
  10. ctrl.TplName = "captcha.html"
  11. if !cpt.VerifyReq(ctrl.Ctx.Request) {
  12. logs.Error("Captcha does not match")
  13. _ = ctrl.Render()
  14. return
  15. }
  16. logs.Info("matched")
  17. _ = ctrl.Render()
  18. }

前端页面可以很简单:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. Hello, world! This is {{.name}}, don't forget to remove the comments to test the form
  9. {{/* <form action="/sendCaptcha" method="post">*/}}
  10. {{/* {{create_captcha}}*/}}
  11. {{/* <input name="captcha" type="text">*/}}
  12. {{/* </form>*/}}
  13. </body>
  14. </html>