Tutorials » AspNet tutorials »

Using Cookies to remember user settings


In this chapter, we will extend the UserTracker project and learn how to use Cookies to remember settings.


What we learned so far ?



In the previous chapters, we learnt how to use session variables and application variables in a web application. Both session variables and application variables are stored in the server memory and will lose the data when web server is reset.

Cookies are stored in client browser. So, it can be stored until user clear the cookies or until it is expired based on the expiry date we specify.

In this chapter, we will display a Label in the Login page which will show the date and time user last logged in to the web site.

Each time the user login again, a new date/time will be stored into the cookie in the client so that next time when user come back, we can surprise him saying when was his last login!

Open the Login page in UserTracker project you already created. You already have several controls in the login page. Just add one more to the Login.aspx page. Note that you must add teh below code within the <Form></Form> tags.


<asp:Label id="lblLastVisit" style="Z-INDEX: 104; LEFT: 458px; POSITION: absolute; TOP: 16px" runat="server" Width="465px"></asp:Label>


This is a label control, which we can use to display the last login date to the user. This control will appear in the top right corner of the page.

Switch to the design mode and ensure that the control is located in the visible portion of the page.

Now the Login page must look as shown below in the design mode.



Add code to save and retrieve cookie information



We have to do 2 things here:

1. Store the date/time and login name into cookie, when user press the "proceed" button.
2. Retrieve values in cookie and display in label control every time the page is loaded.

Go to the event handler btnProceed_Click. Add the following code to the end of the event handler.

' Save current name and time to cookie
Response.Cookies("Name").Value = name
Response.Cookies("Name").Expires = DateTime.Now.AddDays(7)

Response.Cookies("SaveTime").Value = DateTime.Now.ToString()
Response.Cookies("SaveTime").Expires = DateTime.Now.AddDays(7)


The above statements will store 2 variables in the Cookie and set the expiry time as 7 days. This means whatever value is stored in the cookie is valid for 7 days. Since the cookie is stored as physical files in the client machines, it does not matter even if you restart web server or client computer. The cookie can retain it's values.

Now let us add another piece of code in the Page_Load event handler.

Dim nameCookie As HttpCookie = Request.Cookies("Name")
Dim timeCookie As HttpCookie = Request.Cookies("SaveTime")
If nameCookie Is Nothing Then
lblLastVisit.Text = "This is first time you are visiting this stie from this computer."
Else
lblLastVisit.Text = "You last visited this site as '<B><U>" & nameCookie.Value & "</U></B>' on '<B><U>" & timeCookie.Value & "</U></B>'."
End If


The above code will retrieve the name and last login time from the cookies. If it finds some values, it will display the values in the label control.

Compile and run the project now. You must be able to view the last login time every time you launch this application, except for the first time. It can remember the settings for 7 days (the expiry value we set), even if we reboot the machine.

Download sample project



You can download the sample project from here.

If you have any questions about the sample project, please use the feedback link below.




Next Chapter: Validate name in Login page using RequiredFieldValidator control
Previous Chapter: Display currently online users
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India