Codefixer - ASP tutorials, resources and software
ASP.NET 3.5 Hosting – Click Here!
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Friday 9 May
MembersPro PayPal - ASP Membership software

DiscountASP.NET – ASP.NET 3.5/2.0 Hosting


ASP Partner ASP Partner ASP Partner more partners...

Read and write Cookies

A cookie is a small text file written to the visitor’s hard drive that stores user specific information. (Sometimes known as a persistent cookie)

Why use cookies?
Amongst other things Cookies are used to store information about a user, to store usernames and passwords, visited pages, favourites, last visits etc. They allow us to personalize web pages.

How to write a cookie
To write the cookie to the visitors computer we use the Response.Cookies command.

syntax:
Response.Cookies(“CookieName”)=value

How to retrieve a cookie
The "Request.Cookies" command is used to retrieve a cookie value

syntax:Request.Cookies(“CookieName”)

That’s all very basic, Here’s an example of how this website uses a cookie. If you vote in our poll a cookie is written to your computer that stores the poll ID.
Our cookie also has an attribute, in this case a date when the cookie will expire. It's 60 days from now.

<%
'Lets assume for our example that the current poll ID is 19 and assign the value to intPollID
intPollID=19
Response.Cookies("PollId")=intPollID
Response.Cookies("PollId").Expires=Now+60
%>

So this snippet of code above will write a cookie to our visitor's computer if they vote in our poll. IntPollID is a variable that will have been assigned a poll ID value. In this case 19.

How do we retrieve a cookie which will let us know if the visitor has already voted in the current poll when they return to this page? Below is our code which will let us check if the visitor has already voted in this poll.

<%
IntPollID= 19   'Again Lets say for argument sake the current Poll ID is 19
'Check value of the PollID cookie on visitors computer
'& assign it's value to our variable intVoteCastAlready

intVoteCastAlready= Request.Cookies("PollID")
'Check if intVoteCastAlready equals IntPollID
If Cint(intVoteCastAlready)=Cint(IntPollID) Then
Response.write "You have already voted in this current poll"
Else
'you could run code to show poll and let visitor pick an option
End If
%>

What this code is doing is retrieving the current poll ID and storing it in a variable IntPollID, which in the code above is 19. We then try and retrieve the PollID cookie and assign it to the variable intVoteCastAlready. If intVoteCastAlready equals IntPollID then the visitor has already voted in this current poll and we can run code that won't show any poll options just the results for the pole. This will bar the visitor from voting more than once. Vote in our pole and you'll see how this works in practice.

If you do not set the Expires attribute like the example below sets it, the cookie will expire when the user closes their browser.

<%
Response.Cookies(cookieName)="This cookie will last for a year"
Response.Cookies(cookieName).Expires = Now() + 365
%>

Note: The Response.Cookies command must appear BEFORE the <html> tag or if it doesn't you must use <% response.buffer = true %> at the top of your page.

Here's an example of using cookies to see if our visitor has been before.
Another example of using cookies would be a login script. If a user has entered the correct username and password and ticked the 'remember me' checkbox then you could store these in cookies. The next time the visitor came to the login page you could automatically fill in the username and password textboxes with the correct details so that all the visitor would have to do was click the 'submit' button to login.
Here's this scenario in practice. A downside of cookies is that not everyone allows cookies to be written to their computer so just be wary of using them to store critical information and don't let your application be over reliant on them.

Learn how to store mulitple values in a cookie with our 'Cookies with Keys' tutorial.

LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software