ASPCookiesコレクション_
❮完全な応答オブジェクトリファレンス
Cookiesコレクションは、Cookie値を設定または取得するために使用されます。Cookieが存在しない場合は作成され、指定された値を取得します。
注: 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:
|
key | Optional. Specifies the key to where the value is assigned |
例
「Response.Cookies」コマンドは、Cookieを作成するため、またはCookieの値を設定するために使用されます。
<%
Response.Cookies("firstname")="Alex"
%>
上記のコードでは、「firstname」という名前のCookieを作成し、それに値「Alex」を割り当てています。
Cookieの有効期限が切れる日付を設定するなど、Cookieにいくつかの属性を割り当てることもできます。
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2002#
%>
これで、「firstname」という名前のCookieの値は「Alex」になり、2002年5月10日にユーザーのコンピューターから期限切れになります。
「Request.Cookies」コマンドは、Cookie値を取得するために使用されます。
以下の例では、Cookie「firstname」の値を取得してページに表示します。
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
出力:
Firstname=Alex
Cookieには、複数の値のコレクションを含めることもできます。クッキーにはキーがあると言います。
以下の例では、「user」という名前のCookieコレクションを作成します。「ユーザー」Cookieには、ユーザーに関する情報を含むキーがあります。
<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>
以下のコードは、サーバーがユーザーに送信したすべてのCookieを読み取ります。コードは、Cookieに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
❮完全な応答オブジェクトリファレンス