ASP Cookies 컬렉션
Cookies 컬렉션은 쿠키의 값을 설정하거나 가져오는 데 사용됩니다. 쿠키가 존재하지 않으면, 그 값을 설정한 값으로 새 쿠키를 생성합니다.
주의사항:Response.Cookies 명령어는 <html> 태그 앞에 위치해야 합니다.
문법:
Response.Cookies(name)[(key)|.attribute]=value variablename=Request.Cookies(name)[(key)|.attribute]
매개변수 | 설명 |
---|---|
name | 필수. 쿠키의 이름. |
value | 필수(Response.Cookies 명령어에 대해). 쿠키의 값. |
attribute | 선택 사항. 쿠키에 대한 정보를 정의합니다. 다음 중 하나의 매개변수를 사용할 수 있습니다.
|
key | 선택 사항. 값이 할당될 위치를 정의하는 key. |
예제
"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
쿠키는 다중 값을 포함할 수 있습니다. 이를 쿠키가 key를 가지고 있다고 합니다.
아래 예제에서는 "user" 이름의 쿠키 집합을 생성합니다. "user" 쿠키는 사용자 정보를 포함하는 key를 가집니다.
<% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Adams" Response.Cookies("user")("country")="UK" Response.Cookies("user")("age")="25" %>
아래 코드는 서버가 사용자에게 전송한 모든 쿠키를 읽을 수 있습니다. 쿠키가 key를 가지고 있는지�断하기 위해 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=Adams user: country=UK user: age=25