Recommended: ASP instance code: create a long article paging code The following is the quoted content: <%Class aspxsky_page Private Sub class_initialize End Sub Public Function Alert(messa
Cookies are often used to identify users.Example:
| The following is the quoted content: <% 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 first time you 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 %> <html> <body> </body> </html> |
What are cookies?
Cookies are often used to identify users. A cookie is a small file that a server leaves on the user's computer. Whenever the same computer requests the page through the browser, the computer sends a cookie. Through ASP, you can create and retrieve the value of the cookie.
How to create a cookie?
The Response.Cookies command is used to create cookies.
Note: The Response.Cookies command must be preceded by the <html> tag.
In the following example, we create a cookie named firstname and assign it the value of Alex:
| The following is the quoted content: <% Response.Cookies(firstname)=Alex %> |
It is also possible to assign attributes to cookies, such as setting the expiration time of the cookie:
| The following is the quoted content: <% Response.Cookies(firstname)=Alex Response.Cookies(firstname).Expires=#May 10,2002# %> |
How to get 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:
| The following is the quoted content: <% 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 keys (Keys).
In the following example, we will create a cookie set called user. The usercookie has a key containing user information:
| The following is the quoted content: <% 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:
| The following is the quoted content: <% 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 these cookies to a user.
Now, we need to read these cookies. The following example shows you how to do this (note that the code below uses HasKeys to check if the cookie has a key):
| The following is the quoted content: <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:
| The following is the quoted content: firstname=Alex user:firstname=John user:lastname=Smith user:country=Norway user:age=25 |
How to deal with browsers that do not support cookies?
If your application needs to deal with browsers that do not support cookies, you have to use other ways 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:
| The following is the quoted content: <a href=welcome.asp?fname=John&lname=Smith> Go to Welcome Page</a> |
Then retrieve these values in the welcome.asp file similar to the following:
| The following is the quoted content: <% 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 the form
You can also use forms. When the user clicks the submit button, the form submits the data entered by the user to welcome.asp:
| The following is the quoted content: <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> |
Then retrieve these values in the welcome.asp file, like this:
| The following is the quoted content: <% fname=Request.form(fname) lname=Request.form(lname) response.write(<p>Hello & fname & & lname & !</p>) response.write(<p>Welcome to my Web site!</p>) %> |
Share: Some words for ASP and ASP programmers During this period, there have been better discussions about the future of Asp and the quality of Asp. Of course, everyone's hearts are good, but what some friends said is really depressing. Personally, I think that within two years, Asp will be used on many small and medium-sized enterprise B/S systems.