ASP Cookie
- หน้าก่อน ฟอร์ม ASP
- หน้าต่อไป Session ASP
cookie ถูกใช้เพื่อระบุผู้ใช้.
ตัวอย่าง
- cookie ต้อนรับ
- วิธีที่จะสร้าง cookie ต้อนรับ.
อะไรคือ Cookie?
cookie ถูกใช้เพื่อระบุผู้ใช้. cookie คือไฟล์เล็กที่เซิร์ฟเวอร์ที่เก็บในคอมพิวเตอร์ผู้ใช้. ในทุกครั้งที่คอมพิวเตอร์เดียวกันส่งคำขอหน้าผ่านทางบราวเซอร์ คอมพิวเตอร์นั้นจะส่ง cookie ด้วยตนเอง. ผ่าน ASP คุณสามารถสร้างและเรียกค่า cookie ได้.
หากต้องการสร้างคุกกี้?
"Response.Cookies" คำสั่งใช้เพื่อสร้างคุกกี้
แจ้งเตือน:คำสั่ง Response.Cookies ต้องอยู่ก่อนหน้าสัญลักษณ์ <html>
ในตัวอย่างด้านล่างนี้,เราจะสร้างคุกกี้ชื่อ "firstname" และกำหนดค่าเป็น "Alex"
<% Response.Cookies("firstname")="Alex" %>
เราก็สามารถกำหนดคุณสมบัติของคุกกี้ด้วย ตัวอย่างเช่น กำหนดเวลาหมดอายุของคุกกี้
<% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2020# %>
หากต้องการกู้ค่าคุกกี้?
"Request.Cookies" คำสั่งใช้เพื่อกู้ค่าคุกกี้
ในตัวอย่างด้านล่างนี้,เราได้กู้ค่าคุกกี้ชื่อ "firstname" และแสดงค่านี้บนหน้าเว็บ
<% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %>
Output:
Firstname=Alex
คุกกี้ที่มีกุญแจ
หากคุกกี้มีชุดของค่าหลายรายการ พวกเราก็สามารถกล่าวว่าคุกกี้มีกุญแจ (Keys)
ในตัวอย่างด้านล่างนี้,เราจะสร้างคุกกี้ชื่อ "user" ซึ่งมีกุญแจที่มีข้อมูลผู้ใช้
<% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Adams" Response.Cookies("user")("country")="UK" Response.Cookies("user")("age")="25" %>
อ่านคุกกี้ทั้งหมด
โปรดอ่านรหัสด้านล่าง
<% Response.Cookies("firstname")="Alex" 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 />") next else Response.Write(x & "=" & Request.Cookies(x) & "<br />") end if response.write "</p>" next %> </body> </html>
Output:
firstname=Alex user:firstname=John user:lastname=Adams user:country=UK user:age=25
หลักประกันเบราวเซอร์ที่ไม่รับ cookie?
ถ้าโปรแกรมของคุณต้องการจากติดต่อกับเบราวเซอร์ที่ไม่รับ cookie คุณจะต้องใช้วิธีอื่นเพื่อส่งข้อมูลระหว่างหน้าเว็บในโปรแกรมของคุณ มีวิธีสองตัว:
1. เพิ่มค่าประมาณใน URL
คุณสามารถเพิ่มค่าประมาณใน URL ได้:
<a href="welcome.asp?fname=John&lname=Adams"> Go to Welcome Page </a>
จากนั้น รับค่าเหล่านี้ในไฟล์ "welcome.asp" ที่คล้ายดังนี้:
<% fname=Request.querystring("fname") lname=Request.querystring("lname") response.write("<p>Hello " & fname & " " & lname & "!</p>") response.write("<p>Welcome to my Web site!</p>") %>
2. ใช้แบบฟอร์ม
คุณยังสามารถใช้แบบฟอร์มได้。เมื่อผู้ใช้กดปุ่มส่งค่า แบบฟอร์มจะส่งข้อมูลที่ผู้ใช้กรอกไปยัง "welcome.asp":
<form method="post" action="welcome.asp"> First Name: <input type="text" name="fname" value=""> Last Name: <input type="text" name="lname" value=""> <input type="submit" value="Submit"> </form>
จากนั้น ในไฟล์ "welcome.asp" รับค่าเหล่านี้ ตามนี้:
<% fname=Request.form("fname") lname=Request.form("lname") response.write("<p>Hello " & fname & " " & lname & "!</p>") response.write("<p>Welcome to my Web site!</p>") %>
- หน้าก่อน ฟอร์ม ASP
- หน้าต่อไป Session ASP