Anti-bot CAPTCHA
reCAPTCHA
reCAPTCHA protects your website from fraud and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep malicious software from engaging in abusive activities on your website. Meanwhile, legitimate users will be able to login, make purchases, view pages, or create accounts and fake users will be blocked.
Read more at: https://www.google.com/recaptcha/about/
Using with Iris
The reCAPTCHA is a builtin middleware, you don't have to install it.
Import
"github.com/kataras/iris/v12/middleware/recaptcha"Get your public and secret keys from: https://www.google.com/recaptcha/admin
Initialize with
recaptcha.New(recaptchaSecret)Create an HTML form which you want to protect from bots and add the script tag:
<script src="https://www.google.com/recaptcha/api.js"></script>.Add a hidden element or a div with class of
g-recaptchaand set thedata-sitekeyattribute to your public key.Use the middleware to protect the routes.
Example Code:
package main
import (
"fmt"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/recaptcha"
)
// keys should be obtained by https://www.google.com/recaptcha/admin
const (
recaptchaPublic = "6Lf3WywUAAAAAKNfAm5DP2J5ahqedtZdHTYaKkJ6"
recaptchaSecret = "6Lf3WywUAAAAAJpArb8nW_LCL_PuPuokmEABFfgw"
)
func main() {
app := iris.New()
r := recaptcha.New(recaptchaSecret)
app.Get("/comment", showRecaptchaForm)
// pass the middleware before the main handler
// or use the `recaptcha.SiteVerify`.
app.Post("/comment", r, postComment)
app.Listen(":8080")
}
// You can use view templates instead.
var htmlForm = `<form action="/comment" method="POST">
<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="%s"></div>
<input type="submit" name="button" value="Verify">
</form>`
func showRecaptchaForm(ctx iris.Context) {
contents := fmt.Sprintf(htmlForm, recaptchaPublic)
ctx.HTML(contents)
}
// This handler is protected by the recaptcha middleware.
func postComment(ctx iris.Context) {
// [...]
ctx.JSON(iris.Map{"success": true})
}Navigate to http://localhost:8080/comment.
hCaptcha
Stop more bots. Start protecting user privacy. Do you use a captcha to keep out bots? hCaptcha protects user privacy, rewards websites, and helps companies get their data labeled. It is a drop-in replacement for reCAPTCHA: you can switch within minutes.
Read more at: https://www.hcaptcha.com/
Using with Iris
The hCaptcha is a builtin middleware, you don't have to install it.
Import
"github.com/kataras/iris/v12/middleware/hcaptcha"Get your public and secret keys from: https://dashboard.hcaptcha.com.
Initialize with
hcaptcha.New(secretKey)Create an HTML form which you want to protect from bots and add the script tag:
<script src="https://hcaptcha.com/1/api.js" async defer></script>.Add a hidden element or a div with class of
h-captchaand set thedata-sitekeyattribute to your public key.Use the middleware to protect the routes.
Example Code:
For local development you have to setup your HOSTS file:
Let's start by creating our HTML view template form:
The web server should look like this:
Navigate to http://yourdomain.com/register.
Optionally, to get the hCaptcha original response you need to call hcaptcha.Get(ctx). Example:
That's all!
Last updated
Was this helpful?