ASP Cookies
Cookies are often used to identify users.
Try it - Example
Welcome cookie
This example demonstrates how to create a Welcome cookie.
What are Cookies?
Cookies are commonly used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With ASP, you can create and retrieve cookie values.
How to create a cookie?
The "Response.Cookies" command is used to create cookies.
Note: Response.Cookies command must appear before the <html> tag.
In the following example, we will create a cookie named "firstname" and assign it the value "Alex":
Response. Cookies("firstname")="Alex"
%>
It is also possible to assign attributes to cookies, such as setting the cookie expiration time:
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2012
#%>
How to retrieve the value of Cookie?
The "Request.Cookies" command is used to retrieve the value of the cookie.
In the following example, we retrieve the value of the cookie named "firstname" and display the value on the page:
fname =Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
Output: Firstname=Alex
Cookies with Keys
If a cookie contains a collection of multiple values, we can say that the cookie has keys (Keys).
In the following example, we will create a cookie collection named "user". The "user" cookie has a key containing user information:
Response.Cookies("user")("firstname")="John"
Response.Cookies ("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age") ="25"
%>
Read all Cookies
Please read 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 you The server passes all the above cookies to a user.
Now, we need to read all the cookies passed to a user. The following example shows you how to do this (note that the code below checks whether the cookie has keys through the HasKeys property):
< html>
<body>
<%
dim x,y
for 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=Alex
user:firstname=John
user:lastname=Smith
user: country=Norway
user:age=25
What should I do if my browser does not support cookies?
If your application needs to deal with browsers that do not support cookies, then you will have to use other methods to pass information between pages in your application. There are two ways here:
1. Add parameters to the URL
You can add parameters to the URL:
Then retrieve the values in the "welcome.asp" file as follows:
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. Using forms
You can use forms. When the user clicks the Submit button, the form will pass the user input to "welcome.asp":
First Name: <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
< ;input type="submit" value="Submit">
</form>
Then get these values back 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>")
%>