ASP - cookiesverzameling


❮ Volledige referentie van responsobject

De verzameling cookies wordt gebruikt om cookiewaarden in te stellen of op te halen. Als de cookie niet bestaat, wordt deze gemaakt en neemt de waarde aan die is opgegeven.

Opmerking: de opdracht Response.Cookies moet vóór de tag <html> verschijnen.

Syntaxis

Response.Cookies(name)[(key)|.attribute]=value

variablename=Request.Cookies(name)[(key)|.attribute]

Parameter Description
name Required. The name of the cookie
value Required for the Response.Cookies command. The value of the cookie
attribute Optional. Specifies information about the cookie. Can be one of the following parameters: 
  • Domain -  Write-only. The cookie is sent only to requests to this domain
  • Expires - Write-only. The date when the cookie expires. If no date is specified, the cookie will expire when the session ends
  • HasKeys - Read-only. Specifies whether the cookie has keys (This is the only attribute that can be used with the Request.Cookies command)
  • Path - Write-only. If set, the cookie is sent only to requests to this path. If not set, the application path is used
  • Secure - Write-only. Indicates if the cookie is secure
key Optional. Specifies the key to where the value is assigned

Voorbeelden

Het commando "Response.Cookies" wordt gebruikt om een ​​cookie aan te maken of om een ​​cookiewaarde in te stellen:

<%
Response.Cookies("firstname")="Alex"
%>

In de bovenstaande code hebben we een cookie gemaakt met de naam "firstname" en daaraan de waarde "Alex" toegekend.

Het is ook mogelijk om enkele attributen aan een cookie toe te kennen, zoals het instellen van een datum waarop een cookie moet verlopen:

<%
Response.Cookies("firstname")="Alex" 
Response.Cookies("firstname").Expires=#May 10,2002#
%>

Nu heeft de cookie met de naam "firstname" de waarde "Alex" en zal op 10 mei 2002 van de computer van de gebruiker verlopen.

De opdracht "Request.Cookies" wordt gebruikt om een ​​cookiewaarde te krijgen.

In het onderstaande voorbeeld halen we de waarde van de cookie "firstname" op en tonen deze op een pagina:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

Uitgang:
Firstname=Alex

Een cookie kan ook een verzameling van meerdere waarden bevatten. We zeggen dat de cookie Keys heeft.

In het onderstaande voorbeeld zullen we een cookie-verzameling maken met de naam "gebruiker". De "gebruiker"-cookie heeft Sleutels die informatie over een gebruiker bevatten:

<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>

De onderstaande code leest alle cookies die uw server naar een gebruiker heeft gestuurd. Merk op dat de code controleert of een cookie sleutels heeft met de eigenschap HasKeys:

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

Uitgang:

firstname=Alex

user:firstname=John
user:lastname=Smith
user:
country=Norway
user:
age=25


❮ Volledige referentie van responsobject