Tuesday, 29 November 2011

READ ASP COOKIES BASICS

OK, you have a script that writes an asp cookie and now you need to read the cookie each time the user returns to your website. It's really quite simple. First, we declare a few variables and then we use Request.Cookies to read the cookie from the browser and get our values.

For this example, let's say that you have a secure website that uses a username and password combination. When a user becomes a member of your site, you simply write an asp cookie to their browser that stores their username and password and any other data you want. By doing this, you can read the username and password data from the cookie each time the user returns to your site and use another ASP script to validate the cookie data with your database data. As long as the cookie data matches your database data, the user will be able to use your secure web site without any interuption.

Here is how the script looks:

<% DIM strUsername, strPassword, strFirstName, strLastName strUsername = Request.Cookies("MyCOOKIE")("Username") strPassword = Request.Cookies("MyCOOKIE")("Password") strFirstName = Request.Cookies("MyCOOKIE")("strFirstName") strLastName = Request.Cookies("MyCOOKIE")("strLastName") %>

Just replace MyCOOKIE with your own cookie name and you are on your way.
Be sure to choose a unique cookie name to use for your site. If you use a basic name like "MyCOOKIE" like in the example above, the user may visit another website that uses the same cookie name and it could write over your cookie data!

No comments:

Post a Comment