Last active
December 17, 2018 08:00
-
-
Save birowo/19817828a43383894edb258f88dfb26b to your computer and use it in GitHub Desktop.
GOLANG: latihan captcha pakai library https://github.com/mojocn/base64Captcha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"github.com/julienschmidt/httprouter" | |
"github.com/mojocn/base64Captcha" | |
) | |
func main() { | |
r := httprouter.New() | |
r.GET("/captcha", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
//create a captchaInterface instance. | |
//GenerateCaptcha first parameter is empty string,so the package will generate a random uuid for you. | |
idkey, captchaInterfaceIntance := base64Captcha.GenerateCaptcha("", base64Captcha.ConfigCharacter{ | |
Height: 60, | |
Width: 180, | |
//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. | |
Mode: base64Captcha.CaptchaModeNumberAlphabet, | |
ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, | |
ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower, | |
IsUseSimpleFont: false, | |
IsShowHollowLine: false, | |
IsShowNoiseDot: true, | |
IsShowNoiseText: false, | |
IsShowSlimeLine: false, | |
IsShowSineLine: false, | |
CaptchaLen: 6, | |
}) | |
http.SetCookie(w, &http.Cookie{ | |
Name: "idkey", | |
Value: idkey, | |
HttpOnly: true, | |
}) | |
captchaInterfaceIntance.WriteTo(w) | |
}) | |
r.POST("/captcha", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
idkey, _ := r.Cookie("idkey") | |
if base64Captcha.VerifyCaptcha(idkey.Value, r.PostFormValue("captcha")) { | |
w.Write([]byte("verify OK")) | |
} else { | |
w.Write([]byte("verify false")) | |
} | |
}) | |
r.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
w.WriteHeader(200) | |
w.Write([]byte(` | |
<iframe src="/captcha" name="captcha"></iframe><br> | |
<a href="/captcha" target="captcha">coba gambar lain</a><br> | |
<form method="POST" action="/captcha" target="hasil"> | |
ketikkan teks di atas: <input name="captcha"><br> | |
<input type="submit" value="kirim"> | |
</form> | |
<iframe src="about:blank" name="hasil"></iframe> | |
`)) | |
}) | |
http.ListenAndServe(":8080", r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment