ASP cookies usage and Cookies Example tutorial

Source: Internet
Author: User

How do I create a cookie?

In order to create a cookie, you need to use the Response.Cookies command. In the following example, we will create a cookie named "Last Name" and specify the value "somevalue":
<%
Response.Cookies ("lastname") = "Peterson"
%>
The Response.Cookies command must appear in the <HTML> tag, otherwise you need to place the following line at the top of the page:

<% Response.Buffer = True%>

You can also assign a cookie attribute, such as setting a date when the cookie expires. The following example creates a cookie that will expire in 30 days. If you want to leave your visitor as soon as the cookie expires, you must set the Expires attribute with a value of 1.
<%
Response.Cookies ("lastname") = "Peterson"
Response.Cookies ("LastName"). Expires = Now + 30
%>
The next important property is the domain property. This cookie can only read the domain it originated from. This is the default setting where you create the domain, but you can change it as needed. In one example:

<%
Response.Cookies ("LastName"). Domain = "Http://www.webcheatsheet.com"
%>

The other two important attributes are path and security performance. Path property specifies the exact path of the cookie that can be used for the domain.

If the security attribute is set, the cookie can only set whether the browser uses Secure Sockets or HTTPS tutorials://connections, but does not mean that the cookie is safe. It's just a text file like all the other cookies.

In one example:
<%
Response.Cookies ("LastName"). Path = "/cookies/"

Response.Cookies ("LastName"). Secure = True
%>
How do I retrieve the value of a cookie?

Now the cookie settings, we need to retrieve the information. In order to get the value of the cookie, you need to use the Request.Cookies command. In the following example, we retrieve the cookie value named "Last Name" and print out its value.
<%
Somevalue = Request.Cookies ("LastName")
Response.Write ("The cookie value is" & Somevalue)
%>
The output will be "Cookie".

Using a cookie Dictionary

In addition to storing simple values, cookies in the cookie collection can represent a cookie dictionary. A dictionary is an array of constructs that resemble each element in the array that is identified by its name.

Basically, the cookie dictionary is just a cookie that can hold several values. These values are called keys. This gives you a cookie to store all the necessary information options for you. For example, suppose you want to collect user names and store them in a cookie. In the following example, we will create a cookie named "user" that will contain this information
<%
Response.Cookies ("User") ("firstname") = "Andrew"
Response.Cookies ("User") ("lastname") = "Cooper"
%>
When you need to reference the value of the cookie with the key, you must use the key value. In one example:
<%
Response.Write (Request.Cookies ("User") ("FirstName"))
Response.Write (Request.Cookies ("User") ("LastName"))
%>
Now let's assume that we're going to read all of your servers sent to the user's computer on the cookie. In order to check whether there is a key to a cookie or not, you must use a specific cookie to haskeys the property. The following example shows how to do this.

<%
Response.Cookies ("lastname") = "Peterson"
Response.Cookies ("User") ("firstname") = "Andrew"
Response.Cookies ("User") ("lastname") = "Cooper"
%>
<%
' The code below iterates through the Cookies collection.
' If a given cookie represents a cookie dictionary, then
' A second, internal For...each construct iterates through
' It retrieving the value of each cookiekey in the dictionary.

Dim Cookie
Dim Cookiekey

For all cookies in Request.Cookies
If Request.Cookies (cookie). HasKeys Then

    ' The cookie is a dictionary. Iterate through it.
%>
    The cookie dictionary <%=cookie%> has the
    following values : <br/>
<%
    for all cookiekey in Request.Cookies (cookie)
%>
  & nbsp;   &nbsp; &nbsp; Cookiekey: <%= cookiekey%><br/>
      &nbsp; &nbsp; Value:
      <%=request.cookies (Cookie) (Cookiekey)%><br/>
<%
    Next
  else
    ' cookie represents a single value.
%>
  ;   the cookie <%=cookie%> has the following value:
    <%=request.cookies (cookie )%> <br/>
<%
  End If
next
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.