Kolekce souborů cookie ASP


❮ Dokončete referenci objektu odezvy

Kolekce souborů cookie se používá k nastavení nebo získání hodnot souborů cookie. Pokud soubor cookie neexistuje, bude vytvořen a bude mít zadanou hodnotu.

Poznámka: Příkaz Response.Cookies se musí objevit před značkou <html>.

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

Příklady

Příkaz „Response.Cookies“ se používá k vytvoření souboru cookie nebo k nastavení hodnoty souboru cookie:

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

Ve výše uvedeném kódu jsme vytvořili soubor cookie s názvem „firstname“ a přiřadili mu hodnotu „Alex“.

Souboru cookie je také možné přiřadit některé atributy, jako je nastavení data, kdy má soubor cookie vypršet:

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

Nyní má soubor cookie s názvem „firstname“ hodnotu „Alex“ a jeho platnost vyprší z počítače uživatele 10. května 2002.

Příkaz "Request.Cookies" se používá k získání hodnoty cookie.

V níže uvedeném příkladu načteme hodnotu souboru cookie „křestní jméno“ a zobrazíme ji na stránce:

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

Výstup:
Firstname=Alex

Soubor cookie může také obsahovat kolekci více hodnot. Říkáme, že cookie má klíče.

V níže uvedeném příkladu vytvoříme kolekci souborů cookie s názvem „uživatel“. Soubor cookie „uživatel“ má klíče, které obsahují informace o uživateli:

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

Níže uvedený kód přečte všechny soubory cookie, které váš server odeslal uživateli. Všimněte si, že kód kontroluje, zda má cookie klíče s vlastností 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>
%>

Výstup:

firstname=Alex

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


❮ Dokončete referenci objektu odezvy