2011-09-12 29 views
3

我期待創建一個簡單的網頁表單,並且我想「阻止」用戶多次填寫表單。表單的度量用於統計分析,並且每次用戶填寫並重新提交表單時,結果集通常會更改,從而進行分析。限制用戶只填寫一次表格

雖然我們不想阻止重新嘗試(知道重新嘗試已完成也是有價值的信息),但我們確實要警告用戶:「嘿,看起來您最近已經填寫了這些內容。你確定你想再填一遍嗎?「

這裏需要注意的是,我們希望儘量減少收集的可識別個人身份信息的數量。

正在存儲與客戶端的IP最好/最簡單的方法來做到這一點?或者有一種簡單的方法可以將IP服務器端緩存xx時間,因此我們可以進行比較以表示「嗨,我認爲這傢伙今天早些時候試圖訪問我,我應該提出警告」。

+1

也許餅乾是最好的方法;當用戶在代理服務器後訪問應用程序時,IP阻塞可能會導致一些誤報 –

回答

1

具有常數值的cookie應該足夠了,甚至沒有IP。如果用戶沒有清除cookie,你會知道用戶已經填寫了表單。

1

我之前使用的簡單解決方案是在用戶填寫的HTML表單中放置一個不可見的時間戳。如果您提交兩次相同的時間戳,則您知道其重新提交。

如果你擔心篡改,你總是可以混淆/加密時間戳。

這也可能只是一個隨機的唯一標識符,我選擇了一個時間戳,以便知道用戶填寫表單的時間(大致)。

這基本上就像一個會話cookie,但可能被認爲更「私人」,因爲客戶端計算機無法記住它,因此無法像使用某些跟蹤cookie廣告網站一樣使用它。

缺點是這種方法要求客戶端/代理不緩存窗體,因爲它會「改變」每個請求。

0

這裏有兩個問題,用戶點擊提交按鈕多次,用戶在另一個時間點填寫表單。

對於第二個我可以看到兩個相當簡單的解決方案會推薦一個會話cookie,只需cookie用戶機器,不要讓他們再次看到表單,或要求一個信息,如電子郵件地址,然後檢查如果之前使用過數據庫,如果這樣忽略新的表單細節。

對於多表單提交,您可以使用此代碼,這將禁用按鈕onclick。

// 
    // Disable button with no secondary JavaScript function call. 
    // 
    public static void DisableButtonOnClick(Button ButtonControl) 
    { 
     DisableButtonOnClick(ButtonControl, string.Empty); 
    } 

    // Disable button with a JavaScript function call. 
    // 
    public static void DisableButtonOnClick(Button ButtonControl, string ClientFunction) 
    { 
     var sb = new StringBuilder(128); 

     // If the page has ASP.NET validators on it, this code ensures the 
     // page validates before continuing. 
     sb.Append("if (typeof(Page_ClientValidate) == 'function') { "); 
     sb.Append("if (! Page_ClientValidate()) { return false; } } "); 

     // Disable this button. 
     sb.Append("this.disabled = true;"); 

     // If a secondary JavaScript function has been provided, and if it can be found, 
     // call it. Note the name of the JavaScript function to call should be passed without 
     // parens. 
     if (!String.IsNullOrEmpty(ClientFunction)) 
     { 
      sb.AppendFormat("if (typeof({0}) == 'function') {{ {0}() }};", ClientFunction); 
     } 

     // GetPostBackEventReference() obtains a reference to a client-side script function 
     // that causes the server to post back to the page (ie this causes the server-side part 
     // of the "click" to be performed). 
     sb.Append(ButtonControl.Page.GetPostBackEventReference(ButtonControl) + ";"); 

     // Add the JavaScript created a code to be executed when the button is clicked. 
     ButtonControl.Attributes.Add("onclick", sb.ToString()); 
    }