Koleksi Cookie ASP


Referensi Objek Respons Lengkap

Koleksi Cookie digunakan untuk menetapkan atau mendapatkan nilai cookie. Jika cookie tidak ada, itu akan dibuat, dan mengambil nilai yang ditentukan.

Catatan: Perintah Response.Cookies harus muncul sebelum tag <html>.

Sintaksis

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

Contoh

Perintah "Response.Cookies" digunakan untuk membuat cookie atau menetapkan nilai cookie:

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

Dalam kode di atas, kami telah membuat cookie bernama "nama depan" dan menetapkan nilai "Alex" padanya.

Dimungkinkan juga untuk menetapkan beberapa atribut ke cookie, seperti menetapkan tanggal kapan cookie harus kedaluwarsa:

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

Sekarang cookie bernama "nama depan" memiliki nilai "Alex", dan akan kedaluwarsa dari komputer pengguna pada 10 Mei 2002.

Perintah "Request.Cookies" digunakan untuk mendapatkan nilai cookie.

Pada contoh di bawah, kami mengambil nilai cookie "nama depan" dan menampilkannya di halaman:

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

Keluaran:
Firstname=Alex

Cookie juga dapat berisi kumpulan beberapa nilai. Kami mengatakan bahwa cookie memiliki Kunci.

Pada contoh di bawah ini, kami akan membuat kumpulan cookie bernama "pengguna". Cookie "pengguna" memiliki Kunci yang berisi informasi tentang pengguna:

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

Kode di bawah ini membaca semua cookie yang dikirim server Anda ke pengguna. Perhatikan bahwa kode memeriksa apakah cookie memiliki Kunci dengan properti 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>
%>

Keluaran:

firstname=Alex

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


Referensi Objek Respons Lengkap