Hey... I Remember You! - Creating the login page
(Page 4 of 7 )
Before we can remember users credentials, they must login and choose for us to remember them. Never keep a users details without their consent: you'll loose their business for life and they wont come back to your site.
Create a new ASP script and call it login.asp. Add the following code to the page:
1. <html>
2. <head>
3. <title> Login </title>
4. </head>
5.
6. <body bgcolor="#ffffff">
7. <form action="login.asp" method="post">
8. <input type="hidden" name="what" value="checklogin">
9. <h1>Please Login Below</h1>
10. <pre>
11. Username: <input type="text" name="uname" maxlength="20">
12. Password: <input type="password" name="pass" maxlength="20">
13. <input type="checkbox" value="ON"> Remember Me
14. <br><br><input type="submit" value="Login Now">
15. </pre>
16. </form>
17. </body>
18. </html>This page is nearly exactly the same as our adduser.asp form, accept we've changed the action attribute of the form to “login.asp” and the value of the what variable to “checklogin”. We've also added a checkbox. If you're wondering, yes, we will be sending the form post to the same page that generated it (login.asp).
Modify the code that we just created for the login.asp page above so that it looks like this:
1. <%
2.
3. dim strWhat
4. strWhat = Request.Form("what")
5.
6. select case strWhat
7. case "checklogin"
8. CheckLogin()
9. case else
10. ShowLoginForm()
11. end select
12.
13. sub ShowLoginForm()
14.
15. %>
[HTML code goes here]
34. <%
35. end sub
36.
37. sub CheckLogin()
38.
39. dim objConn
40. dim objRS
41. dim strUserName
42. dim strPassword
43. dim strRememberUser
44.
45. strUserName = Request.Form("uname")
46. strPassword = Request.Form("pass")
47. strRememberUser = Request.Form("remember")
48.
49. set objConn = Server.CreateObject("ADODB.Connection")
50. set objRS = Server.CreateObject("ADODB.Recordset")
51.
52. objConn.Open "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=smartlogin; UId=sa; Pwd="
52. objRS.ActiveConnection = objConn
53. objRS.LockType = adLockReadOnly
54. objRS.CursorType = adUseForwardOnly
55. objRS.Open "select count(*) from logins where user_name = '" & strUserName & "' and password = '" & strPassWord & "'"
56. if objRS.Fields(0).Value > 0 then
57. if strRememberUser = "ON" then
58. Response.Cookies("u") = strUserName
59. Response.Cookies("p") = strPassword
60. Response.Cookies(“u”).Expires = Date()+30
61. Response.Cookies(“p”).Expires = Date()+30
else
62. Response.Cookies(“u”).Expires = Date()-1
63. Response.Cookies(“p”).Expires = Date()-1
64. end if
65. Response.Write "Hello " & strUserName & ". You have logged in successfully."
66. else
67. Response.Write "User " & strUserName & " doesn't exist in the logins database."
68. end if
69.
70. end sub
71. %>The layout of our login.asp page is similar to the adduser.asp page. We are checking the value of the Request.Form(“what”) variable and either displaying the form or checking the login credentials posted from the form.
Everyone should be OK with the ShowLoginForm sub routine, so we will talk about the sub routine that will allow our ASP page to check the login details and remember the user if they've chosen it: the CheckLogin routine.
Next: The CheckLogin routine >>
More ASP Articles
More By Mitchell Harper