ASP Cookies 集合
Cookies 集合用于设置或取得 cookie 的值。如果 cookie 不存,就创建它,并赋予它规定的值。
注释:Response.Cookies 命令必须位于 标签之前。
语法:
Response.Cookies(name)[(key)|.attribute]=value variablename=Request.Cookies(name)[(key)|.attribute]
参数 | 描述 |
---|---|
name | 必需。cookie 的名称。 |
value | 必需(对于 Response.Cookies 命令)。cookie 的值。 |
attribute | 可选。规定有关 cookie 的信息。可以是下面的参数之一。
|
key | 可选。规定在何处赋值的 key。 |
उदाहरण
"Response.Cookies" कमांड का उपयोग कोकी बनाने या कोकी के मान को सेट करने के लिए किया जाता है:
<% Response.Cookies("firstname")="Alex" %>
उपरोक्त कोड में, हमने "firstname" नाम की कोकी बनाई है और उसे अलेक्स का मान दिया है。
कोकी के लिए विशेषताएँ भी सेट की जा सकती हैं, जैसे कोकी के अवधारण को सेट करना:
<% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2002# %>
अभी, "firstname" कूकी का मूल्य "Alex" है और उसकी अवधी 2002 मई 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" %>
नीचे दिए गए कोड सभी सर्वर द्वारा उपयोगकर्ता को भेजे गए कूकी को पढ़ सकता है. कूकी के की के मौजूदगी की जांच के लिए हमने 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 /") अगला अन्यथा Response.Write(x & "=" & Request.Cookies(x) & "<br />") अंतः अयः response.write "</p>" अगला %> </body> </html> %>
आउटपुट:
firstname=Alex user:firstname=John user:lastname=Adams user: country=UK user: age=25