選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

ASP Cookies


Cookie 常用於識別使用者。


更多示例

歡迎 Cookie
如何建立歡迎 Cookie。


什麼是 Cookie?

Cookie 常用於識別使用者。Cookie 是伺服器嵌入使用者計算機的一個小檔案。每次同一臺計算機使用瀏覽器請求頁面時,它也會發送 Cookie。使用 ASP,您可以建立和檢索 Cookie 值。


如何建立 Cookie?

"Response.Cookies" 命令用於建立 Cookie。

注意: Response.Cookies 命令必須出現在 <html> 標籤之前。

在下面的示例中,我們將建立一個名為 "firstname" 的 Cookie 併為其賦值 "Alex"

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

還可以為 Cookie 分配屬性,例如設定 Cookie 的過期日期

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

如何檢索 Cookie 值?

"Request.Cookies" 命令用於檢索 Cookie 值。

在下面的示例中,我們檢索名為 "firstname" 的 Cookie 的值並在頁面上顯示它

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

輸出: Firstname=Alex



帶有鍵的 Cookie

如果 Cookie 包含多個值的集合,我們說該 Cookie 具有鍵。

在下面的示例中,我們將建立一個名為 "user" 的 Cookie 集合。"user" Cookie 包含有關使用者的鍵資訊

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

讀取所有 Cookie

檢視以下程式碼

<%
Response.Cookies("firstname")="Alex"
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
%>

假設您的伺服器已將以上所有 Cookie 傳送給使用者。

現在我們要讀取傳送給使用者的所有 Cookie。下面的示例顯示瞭如何操作(請注意,下面的程式碼使用 HasKeys 屬性檢查 Cookie 是否具有鍵)

<!DOCTYPE html>
<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


如果瀏覽器不支援 Cookie 怎麼辦?

如果您的應用程式需要處理不支援 Cookie 的瀏覽器,您將不得不使用其他方法在應用程式的一個頁面到另一個頁面之間傳遞資訊。有兩種方法可以做到這一點:

1. 向 URL 新增引數

您可以向 URL 新增引數

<a href="welcome.asp?fname=John&lname=Smith">轉到歡迎頁面</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="提交">
</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>")
%>

×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援