过滤非法字符
在最近的項目中自己寫了一段處理過濾非法字符的函數
原理:
1:首先判斷頁面的提交方式只有在post的情況下才進行過濾非法字符
2:將所以form表單中的數據保存到一個allvalue中
3:判斷緩存是否過期 過期時從數據庫重新讀取數據,并將其保存到緩存中 未過期則從緩存中讀取數據
4:將allvalue中的值與緩存中的比較判斷是否存在非法字段
?
?
???? public void IfNoKeyWord()
??????? {
??????????? if (HttpContext.Current.Request.HttpMethod == "POST")//form表單提交情況下
??????????? {
??????????????? string allValue = string.Empty;
??????????????? for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
??????????????? {
??????????????????? allValue += HttpContext.Current.Request.Form[i] + "|";//存儲一個頁面的所以form提交的內容
??????????????? }
??????????????? CSqlCommand cmd = new CSqlCommand();
//在頁面緩存過期的情況下,從數據庫中讀取數據;有緩存的情況下從緩存讀取數據
??????????????? DataTable dt?;??????????????
?? if (Cache["LastCache"] == null)
??????????????? {
dt=cmd.getDataTable("select keywords from t_keywords");
??????????????????? Cache.Add("LastCache", dt, null, DateTime.Now.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
??????????????? }
else
??????????????? {
???????????????? dt = (DataTable)Page.Cache["LastCache"];
?????????????????}
?
//對比查找
??????????????????for (int j = 0; j < dt.Rows.Count; j++)
??????????????? {
??????????????????? if (allValue.IndexOf(dt.Rows[j][0].ToString()) > -1)
??????????????????? {
??????????????????????? ZbUtils.alert("你的輸入內容中包含非法字符:【" + dt.Rows[j][0].ToString() + "】!", " window.history.back(-1);", true);
??????????????????? }
??????????????? }
??????????? }
??????? }
?
在頁面basepage初始化的時候調用他
?protected override void OnInit(EventArgs e)
??????? {
??????????? //可更改檢測模式
??????????? if (!CheckPower())
??????????????? return;
??????????? CurrentUserId = Convert.ToInt32(Session["UserID"]);
??????????? CurrentUserName = Session["UserName"].ToString();
??????????? CurrentUserType = Convert.ToInt32(Session["UserType"]);
??????????? IfNoKeyWord();
??????????? base.OnInit(e);
??????? }
總結
- 上一篇: 微型计算机一般不采用的控制方式,微型计算
- 下一篇: WickedEngine 开源游戏引擎