ASP Cookiesコレクション
Cookiesコレクションはcookieの値を設定または取得するために使用されます。cookieが存在しない場合、それを新たに作成し、指定された値を設定します。
コメント:Response.Cookiesコマンドは<html>タグの前に配置する必要があります。
文法:
Response.Cookies(name)[(key)|.attribute]=value variablename=Request.Cookies(name)[(key)|.attribute]
パラメータ | 説明 |
---|---|
name | 必須。cookieの名前。 |
value | 必須(Response.Cookiesコマンド用)。cookieの値。 |
attribute | オプション。cookieに関する情報を指定します。以下のパラメータのいずれかになります。
|
key | オプション。値を設定する場所を指定するkey。 |
例
"Response.Cookies"コマンドは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には値の集合を含むことができます。これをcookieがキーを持つと呼びます。
以下の例では、「user」名のcookieコレクションを作成します。「user」cookieには、ユーザーに関する情報を含むキーがあります。
<% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Adams" Response.Cookies("user")("country")="UK" Response.Cookies("user")("age")="25" %>
以下のコードは、サーバーがユーザーに送信したすべてのcookieを読み取ることができます。HasKeys属性を使用してcookieがキーを持っているかどうかを判断しています:
<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 /") 次へ else Response.Write(x & "=" & Request.Cookies(x) & "<br />") ifエンド response.write "</p>" 次へ %> </body> </html> %>
出力:
firstname=Alex user:firstname=John user:lastname=Adams user: country=UK user: age=25