Collezione di biscotti ASP


❮ Riferimento completo all'oggetto di risposta

La raccolta Cookie viene utilizzata per impostare o ottenere i valori dei cookie. Se il cookie non esiste, verrà creato e assumerà il valore specificato.

Nota: il comando Response.Cookies deve essere visualizzato prima del tag <html>.

Sintassi

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

Esempi

Il comando "Response.Cookies" viene utilizzato per creare un cookie o per impostare un valore del cookie:

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

Nel codice sopra, abbiamo creato un cookie chiamato "firstname" e gli abbiamo assegnato il valore "Alex".

È anche possibile assegnare alcuni attributi a un cookie, come impostare una data di scadenza di un cookie:

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

Ora il cookie denominato "firstname" ha il valore di "Alex" e scadrà dal computer dell'utente il 10 maggio 2002.

Il comando "Request.Cookies" viene utilizzato per ottenere un valore del cookie.

Nell'esempio seguente, recuperiamo il valore del cookie "firstname" e lo visualizziamo su una pagina:

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

Produzione:
Firstname=Alex

Un cookie può anche contenere una raccolta di più valori. Diciamo che il cookie ha le chiavi.

Nell'esempio seguente, creeremo una raccolta di cookie denominata "utente". Il cookie "utente" ha chiavi che contengono informazioni su un utente:

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

Il codice seguente legge tutti i cookie che il tuo server ha inviato a un utente. Si noti che il codice controlla se un cookie ha chiavi con la proprietà 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>
%>

Produzione:

firstname=Alex

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


❮ Riferimento completo all'oggetto di risposta