Beego框架真的很貼心,默認有captcha這個驗證碼插件。在utils/captcha下面
使用方法
~~~
import(
? "github.com/astaxie/beego/cache"
? "github.com/astaxie/beego/utils/captcha"
)
?
var cpt *captcha.Captcha
func init() {
? store := cache.NewMemoryCache()
? cpt = captcha.NewWithFilter("/captcha/", store) //一定要寫在構造函數里面,要不然第一次打開頁面有可能是X
}
~~~
在模板里面寫上
~~~
<form action="/" method="post">
? {{create_captcha}}
? <input name="captcha" type="text">
</form>
~~~
就ok了,最貼心的是居然連onclick事件也已經做在了里面,方便。
還有判斷也已經寫好了,只要在post里面寫上
~~~
if !cpt.VerifyReq(this.Ctx.Request) {
//你的代碼
}
~~~
默認的驗證碼是6位,200px寬,這個是可以自己設置的
cpt是一個結構體:
~~~
// Captcha struct
type Captcha struct {
? // beego cache store
? store cache.Cache
?
? // url prefix for captcha image
? URLPrefix string
?
? // specify captcha id input field name
? FieldIdName string
? // specify captcha result input field name
? FieldCaptchaName string
?
? // captcha image width and height
? StdWidth int
? StdHeight int
?
? // captcha chars nums
? ChallengeNums int
?
? // captcha expiration seconds
? Expiration int64
?
? // cache key prefix
? CachePrefix string
}
~~~
你看到暴露的這些接口了嗎?圖片的大小,字數都是可以調整的,字體、彎曲程度這些就不行。不過寬度也不是可以隨意設置的,我測試的結果是寬度不能小于100,高度不能小于40.不知道是什么情況。
上代碼:
~~~
func init() {
store := cache.NewMemoryCache()
cpt = captcha.NewWithFilter("/captcha/", store)
cpt.ChallengeNums = 4
cpt.StdWidth = 100
cpt.StdHeight = 40
}
~~~
- go環境搭建
- 解決go get網絡慢的問題
- beego的安裝
- bee的安裝
- 編輯器
- go module
- 配置文件詳解
- 配置文件其他說明
- 路由方法
- 路由
- 數據校驗
- 校驗函數
- 頁面跳轉
- 獲取前端數據
- json文件的獲取
- xsrf的用法
- xsrf的防護
- srfs和json的搭配
- flash的用法
- 過濾器
- url反轉
- 各類數據的處理
- 模板函數
- 內置模板函數
- 自定義模板函數
- 模板
- 模板處理
- 模板渲染
- 視圖文件的處理
- 靜態文件
- 請求方式判斷
- 驗證碼
- 另一種方法
- 分頁類
- session
- 登錄判斷
- orm模塊
- 使用方法
- mysql的安裝
- 安裝orm及驅動
- 建立模型
- 自定義模型
- 增刪改查
- 高級查詢
- 常見問題匯總
- 代碼收藏
- 打包部署
- go build打包
- utils收藏
- 新goer容易犯的錯
- 字符串操作