ASP Cookies Collection
❮ การอ้างอิงออบเจ็กต์การตอบสนองที่สมบูรณ์
คอลเล็กชันคุกกี้ใช้เพื่อตั้งค่าหรือรับค่าคุกกี้ หากไม่มีคุกกี้ คุกกี้จะถูกสร้างขึ้นและใช้ค่าที่ระบุ
หมายเหตุ:คำสั่ง 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" ใช้เพื่อสร้างคุกกี้หรือตั้งค่าคุกกี้:
<%
Response.Cookies("firstname")="Alex"
%>
ในโค้ดด้านบนนี้ เราได้สร้างคุกกี้ชื่อ "firstname" และกำหนดค่า "Alex" ให้กับคุกกี้
นอกจากนี้ยังสามารถกำหนดแอตทริบิวต์บางอย่างให้กับคุกกี้ได้ เช่น การตั้งวันที่เมื่อคุกกี้จะหมดอายุ:
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May 10,2002#
%>
ตอนนี้คุกกี้ชื่อ "ชื่อ" มีค่าเป็น "อเล็กซ์" และจะหมดอายุจากคอมพิวเตอร์ของผู้ใช้ในวันที่ 10 พฤษภาคม 2545
คำสั่ง "Request.Cookies" ใช้เพื่อรับค่าคุกกี้
ในตัวอย่างด้านล่าง เราดึงค่าของคุกกี้ "ชื่อ" และแสดงบนหน้า:
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
เอาท์พุท:
Firstname=Alex
คุกกี้ยังสามารถมีคอลเล็กชันของค่าต่างๆ ได้หลายค่า เราว่าคุกกี้มีคีย์
ในตัวอย่างด้านล่าง เราจะสร้างคุกกี้คอลเลกชั่นชื่อ "ผู้ใช้" คุกกี้ "ผู้ใช้" มีคีย์ที่มีข้อมูลเกี่ยวกับผู้ใช้:
<%
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
❮ การอ้างอิงออบเจ็กต์การตอบสนองที่สมบูรณ์