When you have many visitors, you need to use Cookies to store information in your visitors' computers. Basically, what you do is send some bits of information to your clients, and then request it back, when you need to use it.
If you run a search on the Windows Help about cookies, you'll see that it's quite secretive about what they are and how you can set your computer to accept or reject them. As you will see, there are some users who have strong feelings about Cookies, and consider them an intrusion on their privacy. Some complex web sites, such as portals or e-commerce sites, can't work without them.
In order to create a Cookie, you need to place the command Response.Cookies before your <html> tag. You can also assign properties to a Cookie, such as the date when you want it to expire.
<%
Response.Cookies("MyFirstCookie") = "Test";
%>
This creates a cookie, named MyFirstCookie, with the value Test assigned to it, and which expires on the 1st of January 2005. If you want the cookie to expire as soon as your visitor leaves, the Expires property gets the value 1. Cookies that are valid only until the browser window is closed are called in-memory cookies, while those saved on the client's disk are called disk-based cookies.
In order to retrieve the value of the cookie, you will use the command Request.Cookies:
<%
x = Request.Cookies("MyFirstCookie");
Response.Write("MyFirstCookie has the value of " + x)
%>
The output will be "MyFirstCookie has the value of Test"
Using cookies, your page will be able to "identify" the visitors, allowing the offer of personalized content. If a user sends his name, through a form, a cookie can store it and use it later on. It can store other values, such as what links the user accessed, what item he chose from a list, and so on.
A cookie can contain more than one value, in which case these values are called keys:
Response.Cookies("MyFirstCookie")("Key1″) = "Japan";
Response.Cookies("MyFirstCookie")("Key2″) = "China";
Response.Cookies("MyFirstCookie")("Key3″) = "Austrlia";
When you need to reference the values in the cookie with keys, you have to use the key value:
Response.Write(Request.Cookies("MyFirstCookie") ("Key1″));
Response.Write(Request.Cookies("MyFirstCookie") ("Key2″));
Response.Write(Request.Cookies("MyFirstCookie") ("Key3″));
You can use the HasKeys property in order to check if one cookie has keys or not.
Not all browsers support cookies, and you need a way to avoid this problem. One way of doing so is by using forms. Forms, used in HTML, allow you to get some input from the user. In combination with the Request object from ASP, they can be used for a variety of ends. Let's say you want to create a form where the user inputs a name and age.
<form method="post" action="noCookies.asp">
Name: <input type="text" name="name" value="">
Age: <input type="text" name="age" value="">
<input type="submit" value="Submit">
</form>
This puts the forms on your page, and now you need the noCookies.asp file:
<%
name = Request.Form("name");
age = Request.Form("age");
Response.Write("Your name is " + name + " and your age is " + age);
%>
Another way of avoiding browsers that have to cookies is to put your parameters directly into the URL. ASP does not have a built-in method to determine whether a browser has the cookies enabled or not - there are other ways to deal with that problem, but we will not cover them now.
Finally, one last thing about cookies: when you create one, it automatically saves the path of the site that created it. In this way, when the user returns to the site, the cookie follows its path back to the server. You can change this path when you want, something useful one you want one cookie to correspond to two or more applications:
Response.Cookies("MyFirstCookie"). Path="http://server/softwareprojects/application/";
If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?










Leave a Reply