session標籤執行個體:簡單的系統登入代碼(巧妙地避開SQL注入攻擊),sessionsql

來源:互聯網
上載者:User

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的功能,請留意下載最新的版本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.