Skip to content

Commit

Permalink
fix #8
Browse files Browse the repository at this point in the history
  • Loading branch information
hanguofeng committed Jan 9, 2016
1 parent af0cc54 commit 9042423
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
16 changes: 16 additions & 0 deletions captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (captcha *Captcha) GetKey(length int) (string, error) {
info := new(CaptchaInfo)
info.Text = text
info.CreateTime = time.Now()
info.ShownTimes = 0
rst = captcha.store.Add(info)
}
return rst, retErr
Expand Down Expand Up @@ -123,6 +124,21 @@ func (captcha *Captcha) GetImage(key string) (image.Image, error) {
return nil, errors.New("captcha expires")
}

if captcha.captchaConfig.ChangeTextOnRefresh {
if info.ShownTimes > 0 {

text, err := captcha.wordManager.Get(len(info.Text))
if nil != err {
return nil, err
} else {
info.Text = text
}
}

info.ShownTimes++
captcha.store.Update(key, info)
}

cimg := captcha.genImage(info.Text)
return cimg, nil

Expand Down
1 change: 1 addition & 0 deletions captchainfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ import (
type CaptchaInfo struct {
Text string
CreateTime time.Time
ShownTimes int
}
7 changes: 7 additions & 0 deletions confighelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func loadConfigFromFile(configFile string) (error, string, *CaptchaConfig, *Imag

captchaConfig.LifeTime = lifeTime

cfgChangeTextOnRefresh, err := c.Bool("captcha", "change_text_on_refresh")
if nil != err {
cfgChangeTextOnRefresh = false
}

captchaConfig.ChangeTextOnRefresh = cfgChangeTextOnRefresh

//imageConfig
imageConfig := new(ImageConfig)
var fontFiles []string
Expand Down
5 changes: 3 additions & 2 deletions configtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

//CaptchaConfig ,the captcha config
type CaptchaConfig struct {
LifeTime time.Duration
CaseSensitive bool
LifeTime time.Duration
CaseSensitive bool
ChangeTextOnRefresh bool
}

//FilterConfigGroup
Expand Down
1 change: 1 addition & 0 deletions samples/gocaptcha-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gocaptcha-server
1 change: 1 addition & 0 deletions samples/gocaptcha-server/gocaptcha.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[captcha]
word_dict=../../data/en_char
life_time=5m
change_text_on_refresh=true

[service]
port=80
Expand Down

0 comments on commit 9042423

Please sign in to comment.