ASP Beginner's Tutorial: ASP Cookies

Source: Internet
Author: User

ASP Cookies

Cookies are often used to identify users.

Example

<%
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>

Output results.

welcome! This is the the are visiting this Web page.

What is a cookie?
Cookies are often used to identify users. A cookie is a small file server that is embedded on the user's computer. Each time you request a Web browser on the same computer, it will put too many cookies. With ASP, you can create and retrieve cookie values.


-------------------------------------------------- ------------------------------

How do I create a cookie?
The "response.cookies" command is used to create cookies.

Note: The Response.Cookies order must be in court
In the following example, we will create a cookie named "name" and specify the value "Alex" to it:

<%
response.cookies ("firstname") = "Alex"
%>
   

There are also possible cookies, such as determining a date when the cookie should expire:

<%
response.cookies ("firstname") = "Alex"
response.cookies ("FirstName"). expires= #May 10,2002#
%>

How do I retrieve a cookie value?
The "request.cookies" command retrieves a cookie value.

In the following example, we retrieve the value of the cookie named "name" and display it on the Web page:

Fname=request.cookies ("FirstName") Response.Write ("Firstname=" & fname)%>

Output results.

Firstname=alex

The key to a cookie
If a cookie contains a collection of multiple values, we say the cookie is the key.

In the following example, we will create a cookie collection named "User". "User's" cookie has the key, which contains information about the user:

<%
response.cookies ("User") ("firstname") = "John"
response.cookies ("User") ("lastname") = "Smith"
Response.Cookies ("User") ("country") = "Norway"
response.cookies ("User") ("age") = "%>"
Read cookies
<%
response.cookies ("firstname") = "Alex"
response.cookies ("User") ("firstname") = "John"
Response.Cookies ("User") ("lastname") = "Smith"
response.cookies ("User") ("country") = "Norway"
Response.Cookies ("User") ("age") = "%>"
        

Suppose your server has sent all the cookies above the user.

Now, we're going to read all the cookies sent to the user. The following example shows how to do this

(Note that the following code checks whether there is a cookie key with HasKeys property):



         
<%
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>

Transport Division.

Output:

Firstname=alex

User:firstname=john
User:lastname=smith
User:country=norway
User:age=25

If the browser does not support cookies?
If your application involves a browser that does not support cookies, you will have to use other methods to pass information from one page to another in your application. There are two ways to do this:

1. New parameters to a URL
You can add the URL of the parameter

<a href= "Welcome.asp?fname=john&lname=smith" > go to
Welcome page</a>
           

and retrieve the values in the "welcome.asp" file like this:

<%
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
a form you can use. The form is passed to the user input "welcome.asp" when the user clicks the submit button:

 

 <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> 
   
 
                

Fetch The value of the return is in the "welcome.asp" file like this:

 

 <% fname=request.form ("FName") lname=request.form ("LName") re Sponse.write ("<p>hello" & FName & "" & LName & "!</p>") Response.Write ("<p>welcome to My Web site!</p> ")%> 
reprint please indicate from www.111cn.net/asp/asp.html
   
/pre>

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.