在參考了各種資料之後,實現了用戶登錄模塊。主要難點集中在隨機驗證碼的實現以及驗證碼在頁面間的傳遞操作。具體實現代碼如下:
login.asp:
- <%
- FUNCTION GEN_KEY(digits)
- dim char_array(80)
- for i=0 to 9
- char_array(i)=cstr(i)
- next
- for i=10 to 35
- char_array(i)=chr(i+55)
- next
- for i=36 to 61
- char_array(i)=chr(i+61)
- next
- randomize
- do while len(output)<digits
- num=char_array(int((62-0+1)*rnd+0))
- outputoutput=output&num
- loop
- gen_key=output
- end function
- %>
- <html>
- <head></head>
- <body>
- <form name="form1" action="check.asp">
- 用戶登錄界面:
- <BR>
- 用戶名:<INPUT TYPE=TEXTBOX NAME="TXTNAME"></input>
- <BR>
- 密碼:<INPUT TYPE=TEXTBOX NAME="TXTPWD"></input>
- <BR>
- 驗證碼:<INPUT TYPE=TEXTBOX NAME="TXTYZM">
- </INPUT>
- <%
- session("verifycode")=gen_key(4)
- response.write session("verifycode")
- %>
- <BR>
- <input type=submit value="登錄" />
- <input type=reset value="重置" />
- </form>
- </body>
- </html>
check.asp:
- <%
- dim user
- dim pwd
- dim yzm
- user=trim(request("txtname"))
- pwd=trim(request("txtpwd"))
- yzm=trim(request("txtyzm"))
- if user="" or pwd="" then
- response.write "<script>alert('對不起,用戶名和密碼不能爲空!');document.location.href='login.asp';</script>"
- response.end
- else
- if user="lc" then
- if pwd="1234" then
- if session("verifycode")=yzm then
- response.redirect "http://www.baidu.com"
- else
- response.write "驗證碼輸入有誤!"
- end if
- else
- response.write "密碼輸入有誤!"
- end if
- else
- response.write "用戶名不存在!"
- end if
- end if
- %>
爲了使程序簡單易懂,以上實現沒考慮數據庫的連接。,以及驗證碼的刷新問題。