<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                不知道你是否曾經看到過一個論壇或者博客,在一個帖子或者文章后面出現多條重復的記錄,這些大多數是因為用戶重復遞交了留言的表單引起的。由于種種原因,用戶經常會重復遞交表單。通常這只是鼠標的誤操作,如雙擊了遞交按鈕,也可能是為了編輯或者再次核對填寫過的信息,點擊了瀏覽器的后退按鈕,然后又再次點擊了遞交按鈕而不是瀏覽器的前進按鈕。當然,也可能是故意的——比如,在某項在線調查或者博彩活動中重復投票。那我們如何有效的防止用戶多次遞交相同的表單呢? 解決方案是在表單中添加一個帶有唯一值的隱藏字段。在驗證表單時,先檢查帶有該惟一值的表單是否已經遞交過了。如果是,拒絕再次遞交;如果不是,則處理表單進行邏輯處理。另外,如果是采用了Ajax模式遞交表單的話,當表單遞交后,通過javascript來禁用表單的遞交按鈕。 我繼續拿4.2小節的例子優化: ~~~ <input type="checkbox" name="interest" value="football">足球 <input type="checkbox" name="interest" value="basketball">籃球 <input type="checkbox" name="interest" value="tennis">網球 用戶名:<input type="text" name="username"> 密碼:<input type="password" name="password"> <input type="hidden" name="token" value="{{.}}"> <input type="submit" value="登陸"> ~~~ 我們在模版里面增加了一個隱藏字段`token`,這個值我們通過MD5(時間戳)來獲取惟一值,然后我們把這個值存儲到服務器端(session來控制,我們將在第六章講解如何保存),以方便表單提交時比對判定。 ~~~ func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) //獲取請求的方法 if r.Method == "GET" { crutime := time.Now().Unix() h := md5.New() io.WriteString(h, strconv.FormatInt(crutime, 10)) token := fmt.Sprintf("%x", h.Sum(nil)) t, _ := template.ParseFiles("login.gtpl") t.Execute(w, token) } else { //請求的是登陸數據,那么執行登陸的邏輯判斷 r.ParseForm() token := r.Form.Get("token") if token != "" { //驗證token的合法性 } else { //不存在token報錯 } fmt.Println("username length:", len(r.Form["username"][0])) fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username"))) //輸出到服務器端 fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password"))) template.HTMLEscape(w, []byte(r.Form.Get("username"))) //輸出到客戶端 } } ~~~ 上面的代碼輸出到頁面的源碼如下: [![](https://github.com/astaxie/build-web-application-with-golang/raw/master/zh/images/4.4.token.png?raw=true)](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/images/4.4.token.png?raw=true) 圖4.4 增加token之后在客戶端輸出的源碼信息 我們看到token已經有輸出值,你可以不斷的刷新,可以看到這個值在不斷的變化。這樣就保證了每次顯示form表單的時候都是唯一的,用戶遞交的表單保持了唯一性。 我們的解決方案可以防止非惡意的攻擊,并能使惡意用戶暫時不知所措,然后,它卻不能排除所有的欺騙性的動機,對此類情況還需要更復雜的工作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看