deepak
(Deepak Upreti)
July 7, 2017, 6:01pm
1
I want to use captchae in my login form in my login page. I am using “github.com/afocus/captcha ”. and the following code for this purpose:
func captchae(w http.ResponseWriter, req *http.Request) {
pd := captcha.New{
// Must set a font, Other settings have default values
cap.SetFont(“comic.ttf”)
img str := cap.Create(4, captcha.NUM)
}
err := tpl.ExecuteTemplate(w, “admin.gohtml”, pd)
if err != nil {
log.Println(err)
http.Error(w, “Internal server error”, http.StatusInternalServerError)
return
}
}
But it is throwing some error. Can you please suggest me what is the correct way of using captchae in my login page.
Thanks
Deepak
deepak
(Deepak Upreti)
July 10, 2017, 1:23pm
3
Sorry for being late in reply. The error coming is as following:-
go build main.go
command-line-arguments
.\main.go:84: syntax error: unexpected semicolon or newline, expecting comma or
}
My function for Captcha is as following:-
func captchae(w http.ResponseWriter, req *http.Request) {
pd := captcha.New{
//Must set a font, Other settings have default values
cap.SetFont("comic.ttf")
img str := cap.Create(4, captcha.NUM)
}
err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
I think this is what you intended:
func captchae(w http.ResponseWriter, req *http.Request) {
pd := captcha.New()
//Must set a font, Other settings have default values
pd.SetFont("comic.ttf")
img, str := pd.Create(4, captcha.NUM)
err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
captcha.New
is a function, not a struct literal as what you wrote seems to indicate. It takes no arguments
since you called your captca pd
, I renamed the references to cap
cap.Create
returns two values. They require a comma to separate them
deepak
(Deepak Upreti)
July 10, 2017, 2:29pm
5
Thanks a lot for the reply. After making the changes that you have suggested like this:-
func captchae(w http.ResponseWriter, req *http.Request) {
pd := captcha.New()
//Must set a font, Other settings have default values
pd.SetFont("comic.ttf")
img, str := pd.Create(4, captcha.NUM)
err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
log.Println(err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
} }
It is giving this error now:-
go build main.go
command-line-arguments
.\main.go:85: img declared and not used
.\main.go:85: str declared and not used
because you just declared the variables but not using them anywhere
You might want to look at the example code: https://github.com/afocus/captcha/blob/master/examples/main.go
Sadly that example isn’t a complete solution because it doesn’t do everything. My quick thoughts on what a captcha flow requires:
generate the text and image; server remembers the text, sends image to browser
browser displays image
user fills in what the text is; POSTs to server
server compares what the user thinks the text is with the remembered text
goto 1 if incorrect, proceed if correct
Both your code and the example code don’t do all of this.
system
(system)
Closed
October 8, 2017, 3:58pm
8
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.