ASP Cookie
- ບ່ອນໄປກ່ອນ ຟອມຂໍ້ມູນ ASP
- ບ່ອນໄປຫຼັງ ຄູ່ມູນ Session ASP
cookie ຈະໃຊ້ເພື່ອບັນທຶກຜູ້ນຳໃຊ້.
ຕົວຢ່າງ
- cookie ທີ່ຕ້ອນຮັບຜູ້ນຳໃຊ້
- ບວກວ່າຈະສ້າງ cookie �ທີ່ຕ້ອນຮັບຜູ້ນຳໃຊ້.
ວ່າເປັນຫຍັງ Cookie?
cookie ຈະໃຊ້ເພື່ອບັນທຶກຜູ້ນຳໃຊ້. cookie ແມ່ນເປັນເອກະສານນ້ອຍທີ່ຜູ້ບໍລິຫານບໍລິການສົ່ງໄປໃນຄອມພິວເຕີ້ຜູ້ນຳໃຊ້. ເວລາໃດກໍ່ຕາມທີ່ຄອມພິວເຕີ້ດຽວກັນຈະອອກຄຳຮຽກຮ້ອງຂໍ້ມູນຈາກບູ່ເບີ້, ຄອມພິວເຕີ້ນັ້ນຈະສົ່ງ cookie. ຜ່ານ ASP, ທ່ານສາມາດສ້າງແລະເອົາຄູ່ມູນ cookie.
ວິທີທີ່ຈະສ້າງ cookie?
"Response.Cookies" ສຽງສະໜອງຄູ່ມື cookie.
ຄໍາເຫັນ:ສຽງ "Response.Cookies" ຕ້ອງຢູ່ກ່ອນ ປະກອບ <html>.
ໃນຕົວຢ່າງທີ່ຕໍ່ມານີ້،ພວກເຮົາຈະສ້າງ cookie "firstname" ແລະຈັດຄູ່ມື "Alex":
<% Response.Cookies("firstname")="Alex" %>
ກໍ່ສາມາດຈະຈັດວັດຖຸ cookie ທີ່ຈະສຳລັບຄວາມພິສູດຄື ການຈັດວັດຖຸຄວາມສິ້ນສຸດຂອງ cookie:
<% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2020# %>
ວິທີທີ່ທີ່ຈະເອົາຄູ່ມື cookie?
"Request.Cookies" ສຽງສະໜອງຄູ່ມື cookie.
ໃນຕົວຢ່າງທີ່ຕໍ່ມານີ້,ພວກເຮົາຈະເອົາຄູ່ມື "firstname" cookie ແລະສະແດງຂອງຄູ່ມືນີ້ໃນໜ້າເວັບ:
<% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %>
ຄຳອອກສຽງ:
Firstname=Alex
cookie ທີ່ມີຫົວ
ຖ້າ cookie ມີຫຼາຍຄວາມລະອຽດເວລາຫຼາຍຢ່າງໃນກຸ່ມໜຶ່ງແລ້ວ ພວກເຮົາຈະເວົ້າວ່າ cookie ມີຫົວ (Keys).
ໃນຕົວຢ່າງທີ່ຕໍ່ມານີ້,ພວກເຮົາຈະສ້າງ cookie ຄົນ "user". cookie "user" ມີຫົວທີ່ບັນຈຸຂໍ້ມູນຜູ້ນຳໃຊ້:
<% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Adams" Response.Cookies("user")("country")="UK" Response.Cookies("user")("age")="25" %>
ອ່ານສະຫຼົກພວກນີ້ cookie
ການອ່ານລະບຽບດັ່ງຕໍ່ມາ:
<% Response.Cookies("firstname")="Alex" Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Adams" Response.Cookies("user")("country")="UK" Response.Cookies("user")("age")="25" %>
ກ່າວໄດ້ວ່າ ສະຖານນີຂອງເຈົ້າຈະສົ່ງພວກນີ້ cookie ໃຫ້ໃຈຊາຍຄົນໃດຜູ້ໜຶ່ງ.
ຕອນນີ້,ພວກເຮົາຕ້ອງອ່ານສະຫຼົກພວກນີ້ 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 />") next else Response.Write(x & "=" & Request.Cookies(x) & "<br />") end if response.write "</p>" next %> </body> </html>
ຄຳອອກສຽງ:
firstname=Alex user:firstname=John user:lastname=Adams user:country=UK user:age=25
ບວກວ່າບິດຕິດຕໍ່ຄອມພິວເຕີ້ບໍ່ສາມາດຄອກຕີນ?
ຖ້າຄອມພິວເຕີ້ຂອງທ່ານຕ້ອງພະຍາຍາມຮັບການສືບຮູບຄອກຕີນ, ທ່ານຈະຕ້ອງໃຊ້ວິທີອື່ນໆໃນການສົ່ງຂໍ້ມູນລະຫວ່າງໜ້າເວັບໃນຄອມພິວເຕີ້ທີ່ບໍ່ສາມາດຄອກຕີນ. ມີສອງວິທີ:
1. ເພີ່ມປະສົມໃຫ້ URL
ທ່ານສາມາດເພີ່ມປະສົມສຳລັບ URL:
<a href="welcome.asp?fname=John&lname=Adams"> ຢູ່ໃນບາງສາຍເວັບ </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"> ຊື່ຫນັງ: <input type="text" name="fname" value=""> ຊື່ສາຍ: <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