YouTip LogoYouTip

Asp Cookies

* * * Cookies are often used to identify users. * * * ![Image 1: Examples]( ## Try It - Examples ( This example demonstrates how to create a Welcome cookie. * * * ## What is a Cookie? A cookie is often used to identify a user. A cookie is a small file that the server leaves on the user's computer. Each time the same computer requests a page through a browser, it will send the cookie. With ASP, you can create and retrieve the values of cookies. * * * ## How to Create a Cookie? The "Response.Cookies" command is used to create a cookie. **Note:** The Response.Cookies command must appear before the tag. In the following example, we will create a cookie named "firstname" and assign it the value "Alex": It is also possible to assign attributes to the cookie, such as setting the expiration date of the cookie: * * * ## How to Retrieve a Cookie Value? The "Request.Cookies" command is used to retrieve a cookie value. In the following example, we retrieve the value of a cookie named "firstname" and display the value on the page: **Output:** Firstname=Alex * * * ## Cookies with Keys If a cookie contains a collection of multiple values, we can say the cookie has keys (Keys). In the following example, we will create a cookie collection named "user". The "user" cookie has keys containing user information: * * * ## Read All Cookies Please read the following code: Assume your server passes all the cookies above to a user. Now, we need to read all cookies passed to a user. The following example shows you how to do this (note that the code below checks if the cookie has keys using the HasKeys property): <% dim x,y for each x in Request.Cookies response.write("

") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write("
") next else Response.Write(x & "=" & Request.Cookies(x) & "
") end if response.write "

" next %> **Output:** firstname=Alex user:firstname=John user:lastname=Smith user:country=Norway user:age=25 * * * ## What if the Browser Does Not Support Cookies? If your application needs to work with browsers that do not support cookies, then you have to use other methods to pass information between pages in your application. There are two methods: ### 1. Add Parameters to the URL You can add parameters to the URL: Go to Welcome Page Then retrieve these values in the "welcome.asp" file, as follows: <% fname=Request.querystring("fname") lname=Request.querystring("lname") response.write("

Hello " & fname & " " & lname & "!

") response.write("

Welcome to my Web site!

") %> ### 2. Use a Form You can use a form. When the user clicks the Submit button, the form will pass the user input to "welcome.asp": First Name: Last Name: Then retrieve these values in the "welcome.asp" file, as follows: <% fname=Request.form("fname") lname=Request.form("lname") response.write("

Hello " & fname & " " & lname & "!

") response.write("

Welcome to my Web site!

") %>
← Asp SessionsAsp Inputforms β†’