session標籤執行個體:簡單的系統登入代碼(巧妙地避開SQL注入攻擊),sessionsql
提交使用者名稱和密碼到伺服器,以使用者為條件查詢使用者記錄,然後判斷使用者是不是已經註冊,若註冊就判斷密碼是否正確,正確則成功登入,在會話中記錄使用者的相關資訊。查詢中只以使用者名稱為條件,讓資料庫從單列索引中快速找出匹配的使用者記錄,速度遠快於同時使用使用者名稱和密碼作為條件的查詢,而且還巧妙地避開了SQL注入攻擊。
登入頁
表單代碼
<FORM name=form1 action="" method=post onsubmit="return checkval();"> <input name=url value="base/index.html" type=hidden> <TR> <TD align=right width=64 height=25>使用者:</TD> <TD vAlign=top width=112 height=25><INPUT style="FONT-SIZE: 12px; WIDTH: 100px; COLOR: #000000; BACKGROUND-COLOR: #fef7d4" name=USERNAME></TD> <TD vAlign=top width=1> </TD></TR> <TR> <TD align=right width=64 height=25>密碼:</TD> <TD vAlign=top width=112 height=25><INPUT style="MARGIN-TOP: 14px; FONT-SIZE: 12px; WIDTH: 100px; COLOR: #000000; BACKGROUND-COLOR: #fef7d4" type=password name=PASS> </TD> <TD vAlign=top width=1> </TD></TR>
校正的指令碼
var url = location.href;function checkval(){var pos = url.indexOf("index.htm");url = url.substring(0,pos)+"base/check.chtml";form1.action = url;if(form1.USERNAME.value==""){alert("請輸入使用者名稱");form1.USERNAME.focus();return false;} if (form1.PASS.value==""){alert("請輸入密碼");form1.PASS.focus();return false;} } 效果
登入確認頁
查詢
<ESql module=base id=user>Select WE_ID,USERNAME,PASS,CNNAME,DEPT,ACL,PHOTO,PHONE,MOBILE,EMAIL,OICQ,MSN,ENROLLTIME From BASE_USERS Where USERNAME='@{pPage:USERNAME}'</ESql>判斷使用者存在否,不存在則回到表單頁
<if x="@{logic:@{user:getLength}=0}" else=1><we x=true><script>alert("使用者:@{pPage:USERNAME} 尚未註冊!");history.back();</script></we>... ...</if>判斷密碼正確不,不正確則回到表單頁
<if x="@{user:PASS}" else=1><we x="@{pPage:PASS}">... ...</we><script>alert("密碼不對!");history.back();</script></if>密碼正確則登入成功,在會話記錄使用者的相關資訊
<session><we name=WE_ID>@{user:WE_ID}</we><we name=USERNAME>@{user:USERNAME}</we><we name=PASS>@{user:PASS}</we><we name=CNNAME>@{user:CNNAME}</we><we name=DEPT>@{user:DEPT}</we><we name=ACL>@{user:ACL}</we><we name=PHOTO>@{user:PHOTO}</we><we name=PHONE>@{user:PHONE}</we><we name=MOBILE>@{user:MOBILE}</we><we name=EMAIL>@{user:EMAIL}</we><we name=OICQ>@{user:OICQ}</we><we name=MSN>@{user:MSN}</we><we name=ENROLLTIME>@{user:ENROLLTIME}</we></session>跳轉到登入成功後的指定網頁
<script>location.href='@{sys:face}@{pPage:url}';</script>完整代碼
<html><chtml><ESql module=base id=user>Select WE_ID,USERNAME,PASS,CNNAME,DEPT,ACL,PHOTO,PHONE,MOBILE,EMAIL,OICQ,MSN,ENROLLTIME From BASE_USERS Where USERNAME='@{pPage:USERNAME:}'</ESql><if x="@{logic:@{user:getLength}=0}" else=1><we x=true><script>alert("使用者:@{pPage:USERNAME} 尚未註冊!");history.back();</script></we><if x="@{user:PASS}" else=1><we x="@{pPage:PASS}"><session><we name=WE_ID>@{user:WE_ID}</we><we name=USERNAME>@{user:USERNAME}</we><we name=PASS>@{user:PASS}</we><we name=CNNAME>@{user:CNNAME}</we><we name=DEPT>@{user:DEPT}</we><we name=ACL>@{user:ACL}</we><we name=PHOTO>@{user:PHOTO}</we><we name=PHONE>@{user:PHONE}</we><we name=MOBILE>@{user:MOBILE}</we><we name=EMAIL>@{user:EMAIL}</we><we name=OICQ>@{user:OICQ}</we><we name=MSN>@{user:MSN}</we><we name=ENROLLTIME>@{user:ENROLLTIME}</we></session><script>location.href='@{sys:face}@{pPage:url}';</script></we><script>alert("密碼不對!");history.back();</script></if></if></chtml></html>若有不明白之處請在評論中提出,我會與大家一起深入討論
輕開平台資源下載及說明
平台及最新開發手冊免費下載:http://download.csdn.net/detail/tx18/8464425
開發執行個體:輕開B2C電子商務網站,免費下載:http://download.csdn.net/detail/tx18/8318585
輕開平台會不定期升級為大家提供更多強大而Easy的功能,請留意下載最新的版本