ASP 쿠키 컬렉션


❮ 완전한 응답 객체 참조

쿠키 컬렉션은 쿠키 값을 설정하거나 가져오는 데 사용됩니다. 쿠키가 존재하지 않으면 쿠키가 생성되고 지정된 값을 사용합니다.

참고: Response.Cookies 명령은 <html> 태그 앞에 나타나야 합니다.

통사론

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

"Response.Cookies" 명령은 쿠키를 생성하거나 쿠키 값을 설정하는 데 사용됩니다.

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

위의 코드에서 "firstname"이라는 이름의 쿠키를 만들고 "Alex" 값을 할당했습니다.

쿠키가 만료되어야 하는 날짜 설정과 같은 일부 속성을 쿠키에 할당하는 것도 가능합니다.

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

이제 "firstname"이라는 이름의 쿠키는 "Alex" 값을 가지며 2002년 5월 10일에 사용자의 컴퓨터에서 만료됩니다.

"Request.Cookies" 명령은 쿠키 값을 가져오는 데 사용됩니다.

아래 예에서는 쿠키 "firstname"의 값을 검색하여 페이지에 표시합니다.

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

산출:
Firstname=Alex

쿠키에는 여러 값 모음이 포함될 수도 있습니다. 쿠키에 키가 있다고 말합니다.

아래 예에서는 "user"라는 쿠키 컬렉션을 만듭니다. "사용자" 쿠키에는 사용자에 대한 정보가 포함된 키가 있습니다.

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

아래 코드는 서버가 사용자에게 보낸 모든 쿠키를 읽습니다. 이 코드는 쿠키에 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>
%>

산출:

firstname=Alex

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


❮ 완전한 응답 객체 참조