Active Members Fi8sVrs Posted December 16, 2011 Active Members Report Posted December 16, 2011 What is a Cookie?A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.How to Create a Cookie?The "Response.Cookies" command is used to create cookies.Note: The Response.Cookies command must appear BEFORE the <html> tag.In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:<%Response.Cookies("firstname")="Alex"%>It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:<%Response.Cookies("firstname")="Alex"Response.Cookies("firstname").Expires=#May 10,2012#%>How to Retrieve a Cookie Value?The "Request.Cookies" command is used to retrieve a cookie value.In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:<%fname=Request.Cookies("firstname")response.write("Firstname=" & fname)%>Output: Firstname=AlexA Cookie with KeysIf a cookie contains a collection of multiple values, we say that the cookie has Keys.In the example below, we will create a cookie collection named "user". The "user" cookie has Keys that contains information about a user:<%Response.Cookies("user")("firstname")="John"Response.Cookies("user")("lastname")="Smith"Response.Cookies("user")("country")="Norway"Response.Cookies("user")("age")="25"%>Read all CookiesLook at the following code:<%Response.Cookies("firstname")="Alex"Response.Cookies("user")("firstname")="John"Response.Cookies("user")("lastname")="Smith"Response.Cookies("user")("country")="Norway"Response.Cookies("user")("age")="25"%>Assume that your server has sent all the cookies above to a user.Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):<html><body><%dim x,yfor each x in Request.Cookies response.write("<p>") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write("<br />") next else Response.Write(x & "=" & Request.Cookies(x) & "<br />") end if response.write "</p>"next%></body></html>Output:firstname=Alexuser:firstname=Johnuser:lastname=Smithuser:country=Norwayuser:age=25What if a Browser Does NOT Support Cookies?If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. There are two ways of doing this:1. Add parameters to a URLYou can add parameters to a URL:<a href="welcome.asp?fname=John&lname=Smith">Go to Welcome Page</a>And retrieve the values in the "welcome.asp" file like this:<%fname=Request.querystring("fname")lname=Request.querystring("lname")response.write("<p>Hello " & fname & " " & lname & "!</p>")response.write("<p>Welcome to my Web site!</p>")%>2. Use a formYou can use a form. The form passes the user input to "welcome.asp" when the user clicks on the Submit button:<form method="post" action="welcome.asp">First Name: <input type="text" name="fname" value="" />Last Name: <input type="text" name="lname" value="" /><input type="submit" value="Submit" /></form>Retrieve the values in the "welcome.asp" file like this:<%fname=Request.form("fname")lname=Request.form("lname")response.write("<p>Hello " & fname & " " & lname & "!</p>")response.write("<p>Welcome to my Web site!</p>")%>Source w3schools.com/ASP/asp_cookies.asp 1 Quote