In my opinion, one of the most annoying things about some websites is logging in. Today, Mitchell will demonstrate how to create a nifty little feature that will allow your site to "remember" its visitors.
Hey... I Remember You! - The CheckLogin routine (Page 5 of 7 )
The CheckLogin routine is the core of our ASP application and will handle the validation of the user credentials as well as the code that will help our ASP scripts remember the current user.
Starting on line 45, we are getting the values of the username and password that the user has submitted via the form. On line 46, the strRemember variable will be set to “ON” if the “Remember Me” checkbox was checked on the login form.
Lines 52 to 54 open a connection to our database server and declare a new recordset object. The LockType and CursorType of the recordset object are set to adLockReadOnly and adUseForwardOnly respectively. These are imported in line 1 via the use of a meta tag. This special meta tag extracts all of the variables from the MSADO15.dll file and lets our ASP script use them as if we had manually declared them ourselves.
Because we have set both LockType and CursorType values for our recordset, it will only be able to fetch records in a forward seeking motion. This greatly reduces the time it takes to fetch records from the database, because SQL doesn't have to create any locks or temporary data for modified or new records.
Line 55 executes a SELECT command to get a count of the number of users whose credentials match those passed from our login form and stores the result in our recordset object.
On line 56, we check how many users the SQL COUNT() function returned. Because we used a “select count(*)” query, our recordset will contain just one row with one field containing the number of users that matched the credentials we supplied.
On line 57, if at least one user was found matching the credentials from the login form, we check whether or not they have chosen to be remembered. If they have, (strRememberUser = “ON”) then we set two cookie values containing the username and password values (u and p). If they haven't, we wont set the cookie values, but we will set their expiry date to yesterday, which will remove them from the users machine if they existed previously. Alternatively, lines 60 and 61 set the expiry date of our username and password cookies to 30 days from now if the user has chosen to be remembered.
Lastly, if the login was successful, our script will write one line to the browser: “Hello John. You have logged in successfully”. On the other hand, if the login fails, our ASP script will tell the user that their credentials weren't found in the database.