當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

JavaScript過濾SQL注入字符

JavaScript過濾SQL注入字符

更新時間:2023-06-21 文章作者:未知 信息來源:網絡 閱讀次數:

編程(Programming)是編定程序的中文簡稱,就是讓計算機代碼解決某個問題,對某個計算體系規定一定的運算方式,使計算體系按照該計算方式運行,并最終得到相應結果的過程。為了使計算機能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計算機能夠理解的形式告訴計算機,使得計算機能夠根據人的指令一步一步去工作,完成某種特定的任務。這種人和計算體系之間交流的過程就是編程。

【實例名稱】

JavaScript 過濾 SQL 注入字符

【實例描述】

由于大多數的數據提交語句都是通過多個字符串進行連接,所以為了防止SQL 的注入字符,本例介紹一種過濾特殊字符的方法。 


【實例代碼】

<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>標題頁</title> <script LANGUAGE="JavaScript"> function check(inputStr) {    if (typeof(inputStr) != "string") { return inputStr; } //判斷是否是字符串類型    var tmpValue = inputStr;     //以下搜索字符串中的特殊字符,如果存在,則替換成""     while (tmpValue.indexOf(';') > -1) {tmpValue = tmpValue.replace(';',''); }     while (tmpValue.indexOf('<') > -1) {tmpValue = tmpValue.replace('<',''); }     while (tmpValue.indexOf('>') > -1) {tmpValue = tmpValue.replace('>',''); }     while (tmpValue.indexOf('--') > -1) {tmpValue = tmpValue.replace('--',''); }    while (tmpValue.indexOf(",") > -1) {tmpValue = tmpValue.replace(",",""); }    while (tmpValue.indexOf("'") > -1) {tmpValue = tmpValue.replace("'",""); }    while (tmpValue.indexOf("?") > -1) {tmpValue = tmpValue.replace("?",""); }    document.getElementById("txt1").value = tmpValue;  //重新顯示更改后的變量 } </script> </head> <body> <input type=text id="txt1" value="select * from userinfo where username=zhang' and passwrod=2" style="width: 392px"> <input type=button value="提交" onClick="check(txt1.value)"> </body> </html>

【運行效果】

運行效果

【難點剖析】

本例的重點是對特殊字符的判斷。sQL需要防范的注入字符在代碼中被一一列舉,使用“indexOF”方法,可以判斷字符串中是否包含這些字符。如果“indexOf”返回“一1”,則表示不包含指定的特殊字符。

【源碼下載】

本實例JS代碼下載

 


使用編程語言寫的程序,由于每條指令都對應計算機一個特定的基本動作,所以程序占用內存少、執行效率高。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
亚洲嫩草影院久久精品