Sammlung von ASP -Cookies


❮ Vollständige Antwortobjektreferenz

Die Cookies-Sammlung wird verwendet, um Cookie-Werte festzulegen oder abzurufen. Wenn das Cookie nicht vorhanden ist, wird es erstellt und nimmt den angegebenen Wert an.

Hinweis: Der Befehl Response.Cookies muss vor dem <html>-Tag stehen.

Syntax

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

Beispiele

Der Befehl "Response.Cookies" wird verwendet, um ein Cookie zu erstellen oder einen Cookie-Wert zu setzen:

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

Im obigen Code haben wir ein Cookie mit dem Namen „firstname“ erstellt und ihm den Wert „Alex“ zugewiesen.

Es ist auch möglich, einem Cookie einige Attribute zuzuweisen, wie z. B. das Festlegen eines Datums, an dem ein Cookie ablaufen soll:

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

Jetzt hat das Cookie mit dem Namen „Vorname“ den Wert „Alex“ und läuft am 10. Mai 2002 vom Computer des Benutzers ab.

Der Befehl "Request.Cookies" wird verwendet, um einen Cookie-Wert zu erhalten.

Im folgenden Beispiel rufen wir den Wert des Cookies „firstname“ ab und zeigen ihn auf einer Seite an:

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

Ausgabe:
Firstname=Alex

Ein Cookie kann auch eine Sammlung mehrerer Werte enthalten. Wir sagen, dass das Cookie Schlüssel hat.

Im folgenden Beispiel erstellen wir eine Cookie-Sammlung mit dem Namen „Benutzer“. Das „Benutzer“-Cookie enthält Schlüssel, die Informationen über einen Benutzer enthalten:

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

Der folgende Code liest alle Cookies, die Ihr Server an einen Benutzer gesendet hat. Beachten Sie, dass der Code prüft, ob ein Cookie Schlüssel mit der HasKeys-Eigenschaft hat:

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

Ausgabe:

firstname=Alex

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


❮ Vollständige Antwortobjektreferenz