Web Design Tutorials ASP web site design tutorial
Learn How to Use Coo
Learn How to Use Cookies in ASP
Learn how to use cookies in ASP.
Setting and playing around with cookies is a fun and useful way to save data on a user's hard drive, and can successfully store valuable information which may be helpful the next time they come to the site. It's fairly simple to set up, and even easier to read. To use it, you have to remember some guidelines:
1. You must put the cookie before any HTML.
2. The cookie will not work on the page until its refreshed, or the user visits the page again (as long as the cookie hasn't expired).
3. The cookie only works on the directory you're in and down. Its best to place the cookie in a file which is located in the root directory, so you can use the cookie anywhere around the site.
Here's the code to set a variable:
<%
Response.Cookies("cookie")="spoono rocks!"
response.write Request.Cookies("cookie") 'prints "spoono rocks!"
%>
|
Now, the next time someone visits this page, or any other ASP page in the same directory or a child directory, the Response.Cookies("cookie") variable will already be defined. If you response.write Request.Cookies("cookie") it will display "spoono rocks!".
Moving on, if you want the cookie to expire past the closing of the browser, you can use this code (where 365 is the number of days):
<%
Response.Cookies("cookie")="spoono rocks!"
Response.Cookies("cookie").Expires=date+365
response.write Request.Cookies("cookie") 'prints "spoono rocks!" for the next year
%>
|
Some people also want to know how to store multiple variables in one cookie. Not a problem, we'll be using Keys to identify the different variables. Remember: a cookie can either have keys or not, never both.
<%
Response.Cookies("cookie")("one")="spoono did rock!"
Response.Cookies("cookie")("two")="spoono is rocking!"
Response.Cookies("cookie")("three")="spoono will rock!"
response.write Request.Cookies("cookie")("one") 'prints "spoono did rock!"
response.write Request.Cookies("cookie")("two") 'prints "spoono is rocking!"
response.write Request.Cookies("cookie")("three") 'prints "spoono will rock!"
%>
|
Finally, you may wonder how to make the cookie "dissappear" when you don't want it anymore? Eat it:
<%
Response.Cookies("cookie")=""
%>
|
Thats it! Not too challenging was it?
| Rate This Material: |
Bad |
Excellent |
|
| |

© 2010 www.kagemedia.com
Kelowna web design and development | Kelowna web hosting
Kelowna website marketing | Kelowna database design |
Links
Serving:
Kelowna BC - Penticton BC - Vernon BC - Westbank BC - Winfield BC
|
|
 |
Win a FREE web design!
We are giving away a completely free web design and web hosting package every month.
Click here for details |
|
|
|