ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
ASP.NET Mobile Controls: Tutorial Guide: Adaptive Web Content for Mobile Devices with the MMIT
ASP.NET Mobile Controls: Tutorial Guide: Adaptive Web Content for Mobile Devices with the MMIT

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

Advanced Cookie Techniques with ASP

There are many articles on the web that explain how to use cookies in your webpage’s. Most of these articles only cover the very basics and don’t give any examples that are practical if you are working with a database. I have had some very unique requests from clients for which I had to use both session state cookies and client side cookies. I know a lot of people frown on the use of either, but there are cases when you have to use them. I will discuss a few situations in which you would use cookies in your code.

How do I use cookies to log a user into my site when he/she returns? With cookies of course! First let’s start with the basics.

To write a cookie we use the response.cookies collection. When you call it from a webpage it will write the information to the “cookies” folder on the target PC. When working with cookies it is vital to find this directory and become familiar with what the content of a cookie looks like. I will assume that you already have a login system in place on your website therefore this code would just be an addition to it. After you have verified that the username and password is correct you would then code the section to write the cookie. It is usually best to have a separate form field to allow them to choose whether or not they want to store a cookie.

<%


if request.form(“savecookie”) = “Yes” and validlogin = “Yes” then

Response.cookies(“member”)(“username”) = request.form(“username”)
Response.cookies(“member”)(“password”) = request.form(“password”)
Response.Cookies("member").Expires = DATE + 365

End if

%>

The cookie “member” has a “username” key and a “password” key. You can define as many keys in a cookie as you want as long as your cookie file stays below 4k. As a general rule I like to keep my cookie files at about 1k or below for optimal performance. If you want to store data for longer than just a browser session then you will have to specify the “expires” property of the cookie. The cookie above will expire 1 year from the date of creation. If you find that your cookies are not acting stable and they are losing information you might try to reduce the amount of information you are storing in them.

Now after you have stored the information on the PC you will need to retrieve that data in order to log them in when they return. To do this we use the standard request object to get the cookie data.

<%


un = request.cookies(“member”)(“username”)
pw = request.cookies(“member”)(“password”)
‘check to see if the username and password are in the cookie
if un <> “” and pw <> “” then

set rs = conn.execute(“select * from members where username = “&un&””)
if rs.eof = false then
if rs(“password”) = pw then
session(“loggedin”) = rs(“ID”)
end if
end if
end if


%>

In the above example we check the username and password against the database and if they match we create a browser session called “loggedin”. You can then use this session variable throughout their visit to query a database.

One problem with this method is that the username and password is being stored in clear text on the local machine. Despite that, it is still the best way to have an automated login on your website.

If you want to allow the user to delete the cookie or “logout” then just include this simple bit of code to erase all the key values from the cookie.

<%


if logout = "Yes" then
response.cookies("member") = " "

end if


%>

You have probably noticed on some forum sites that a common feature is the ability to show whether or not there are new posts since your last visit. There are many different methods to accomplish this kind of task on your page. In my example I will use the comparison of database field values.

A client I worked with wanted something a little different than the basic forums you see around. He had a listing of news articles that users could post comments to. He wanted it to show a star beside any news articles that had new posts since the user last visited. He wanted the star to go away only after the user read the post. Most forums just reset all the cookies values once you have visited. By doing it this way, a user will know which news articles he has not read no matter how many times he returns back to the site. The key here is to only update the cookie values once the user has clicked on a link.

<%response.buffer = true%>
It is important to include the response.buffer at the top of your page if you are going to be working with cookies inside the content of the page.
<%

' here I open a recordset for the news items and loop through it
set rs = conn.execute("select top 10 * from news")
do while not rs.eof
'count the comments made for each news item
set rscount = conn.execute("select count(*) as cc from comments where newsID = "&rs("newsID")&"")

'output html to the browser
response.write(""&rs("title")&"")

'here we check to see if a key exists for the news item based on the newsID in
'the database. If a key does not exist, we create one based on the count of comments
'in the database for that news item.

if request.cookies("news")(""&rs("newsID")&"") = "" then
response.cookies("news")(""&rs("newsID")&"") = ""&rscount("cc")&""
response.cookies("news").Expires = date + 365
rs.movenext
end if

rs.close
rscount.close
set rs = nothing
set rscount = nothing

loop


%>

Obviously there can be a multitude of ways to implement something like this. What I am doing is storing a value for the amount of comments in each news item. All you need to do is compare the value of the cookie against the database to decide whether or not to show a star on the post. Next, you will want to reset the cookie value if the user clicks to read the post. On the top of the corresponding page place a block of code similar to the following.

<%


'update cookie if user views the post
if request.querystring("newsID") <> "" then
set rs = conn.execute("select count(*) as cc from comments where newsID = "&request.querystring("newsID")&"")
response.cookies("news")(""&request.querystring("newsID")&"") = ""&rs("cc")&""
Response.Cookies("news").Expires = date + 365
rs.close
set rs = nothing
end if


%>

If you were to combine all of these ideas together on a site here is what a cookie file might resemble.

Obviously your cookie will look different depending on how you structure your code. You can see in the above example that the cookie "news" has several keys. The first two keys are 160 and 141. In this example the key value is the amount of comments related to the news article. Article number 160 has 0 comments and article 141 has 18 comments.

I hope you have gained some insight on how to implement cookies into your sight. If you have any questions about the implementation of this code please feel free to contact me.


 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 12/1/2008 10:54:51 AM