Cookie| Program | Basic Tutorials
Cookies are often used to identify users.
Instance:
<%
Dim numvisits
Response.Cookies ("NumVisits"). expires=date+365
Numvisits=request.cookies ("NumVisits")
If numvisits= "" Then
Response.Cookies ("NumVisits") =1
Response.Write ("welcome! This is the the ' the ' are visiting this Web page.
Else
Response.Cookies ("NumVisits") =numvisits+1
Response.Write ("You have visited this")
Response.Write ("Web page" & NumVisits)
If Numvisits=1 Then
Response.Write "Time before!"
Else
Response.Write "Times before!"
End If
End If
%>
<body>
</body>
What is a cookie?
Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. The computer sends cookies whenever the same computer requests a page through a browser. With ASP, you can create and retrieve the value of a cookie.
How do I create cookies?
The "response.cookies" command is used to create cookies.
Note: The Response.Cookies command must precede the
In the following example, we create a cookie named "FirstName" and assign it to "Alex":
<%
Response.Cookies ("firstname") = "Alex"
%>
It is also possible to assign properties to cookies, such as setting the expiration time for cookies:
<%
Response.Cookies ("firstname") = "Alex"
Response.Cookies ("FirstName"). expires= #May 10,2002#
%>
How do I get back the value of a cookie?
"Request.Cookies" commands the user 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 series of multiple values, we can say that the cookie has a key (keys).
In the following example, we will create a cookie set named "User". The "user" cookie has a key that contains 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"
%>
Suppose your server passes all of these cookies to a user.
Now, we need to read these cookies. The following example shows you how to do this (note that the following code uses HasKeys to check whether a cookie has keys):
<body>
<%
Dim x,y
For each x in Request.Cookies
Response.Write ("<p>")
If Request.Cookies (x). HasKeys Then
For all 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>
Output:
Firstname=alex
User:firstname=john
User:lastname=smith
User:country=norway
User:age=25
How do I respond to browsers that do not support cookies?
If your application needs to deal with browsers that do not support cookies, you will have to use other methods to pass information between pages in your application. Here are two ways:
1. Add parameters to the URL
You can add parameters to the URL:
<a href= "Welcome.asp?fname=john&lname=smith" >
Go to Welcome page</a>
These values are then retrieved in a "welcome.asp" file similar to the following:
<%
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 also use the form. When the user clicks the Submit button, the form submits the user's input to "welcome.asp":
<form method= "POST" action= "welcome.asp" >
Name: <input type= "text" name= "fname" value= "" >
Last Name: <input type= "text" name= "lname" value= "" >
<input type= "Submit" value= "Submit" >
</form>
Then retrieve these values in the "welcome.asp" file, just 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>")
%>