ASP Cookie

cookie 常用来对用户进行识别。

实例

Welcome cookie
如何创建欢迎 cookie。

什么是 Cookie?

cookie 常用来对用户进行识别。cookie 是一种服务器留在用户电脑中的小文件。每当同一台电脑通过浏览器请求页面时,这台电脑也会发送 cookie。通过 ASP,您能够创建并取回 cookie 的值。

如何创建 cookie?

"Response.Cookies" အမိန့် သည် အားကစားအမျိုးအစား ကို ဖွင့်ပေးရန် အသုံးပြုသည်。

အကြောင်းကြားငြား:Response.Cookies အမိန့် သည် <html> အချက်အလက် အောက်မှ သုံးပါလိမ့်မည်。

အောက်အရာများတွင်၊ ကျွန်ုပ်တို့သည် "firstname" အမည်ရှိ အားကစားအမျိုးစား ကို ဖွင့်ပေးလိမ့်မည် နှင့် "Alex" ကို ချိတ်ဆက်ပါလိမ့်မည်:

<%
Response.Cookies("user")("firstname")="Alex"
%>

အားကစားအမျိုးအစား အချက်အလက်များ ကို ချိတ်ဆက်ရန် လည်း အခွင့်ရှိပါသည်၊ ဥပမာ၊ အားကစားအမျိုးအစား အား ပျက်ကွက်ချိန် ကို အစားထိုးရန်:

<%
Response.Cookies("user")("firstname")="Alex" 
Response.Cookies("firstname").Expires=#May 10,2020#
%>

အားကစားအမျိုးအစား အား မြင်တွေ့ရန် ဘယ်လိုဖြစ်သလဲ?

"Request.Cookies" အမိန့် သည် အားကစားအမျိုးအစား အား မြင်တွေ့ရန် အသုံးပြုသည်。

အောက်အရာများတွင်၊ ကျွန်ုပ်တို့သည် "firstname" အမည်ရှိ အားကစားအမျိုးအစား ကို နှင့် အားကစားအမျိုးစား ကို ပြန်လည်ရယူပြီး စာမေးပါက ပြထားပါလိမ့်မည်:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

输出:

Firstname=Alex

ကြိတ်သားပါဝင်သော အားကစားအမျိုးအစား

အားကစားအမျိုးအစားများ တစ်ခုခုပါး များစွာသော အရာများ ကို ပါဝင်သော အားကစားအမျိုးအစား တစ်ခု သည် ကြိတ်သား (Keys) ကို ပါဝင်သည် ဟု ပြောနိုင်ပါသည်。

အောက်အရာများတွင်၊ ကျွန်ုပ်တို့သည် "user" အမည်ရှိ အားကစားအမျိုးအစား အစိုးရွက် ကို ဖွင့်ပေးလိမ့်မည် "user" အားကစားအမျိုးအစား အစိုးရွက် သည် အသုံးစွဲသူ၏ သတင်းအချက်အလက်များ ပါဝင်သော ကြိတ်သားများ ရှိသည်:

<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Adams"
Response.Cookies("user")("country")="UK"
Response.Cookies("user")("age")="25"
%>

အားကစားအမျိုးအစားများ အားလုံး ဖတ်ရှုရန်

အောက်အရာများကို ဖတ်ရှုပါလိမ့်မည်:

<%
Response.Cookies("user")("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("
") next else Response.Write(x & "=" & Request.Cookies(x) & "
") end if response.write "

" next %>

输出:

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">
ဝေးကွာသော စာမေးပစ္စည်း သို့ သွားသည်
</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>")
%>