![]() |
|
|
Read and write CookiesA 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? 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. <% 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. <% 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. <% 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.
Site developed by Michael Wall - Web Design Belfast N.Ireland. |
|