asp初級教程:ASP Cookies

來源:互聯網
上載者:User

ASP Cookies

Cookie是經常被用來識別使用者。

範例

<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")

if numvisits="" then
   response.cookies("NumVisits")=1
   response.write("Welcome! This is the first time you are visiting this Web page.")
else
   response.cookies("NumVisits")=numvisits+1
   response.write("You have visited this ")
   response.write("Web page " & numvisits)
   if numvisits=1 then
     response.write " time before!"
   else
     response.write " times before!"
   end if
end if
%>
<html>
<body>
</body>
</html>
輸出結果.

Welcome! This is the first time you are visiting this Web page.

cookie是什嗎?
Cookie是經常被用來識別使用者。 Cookie是一個小型的檔案伺服器嵌入在使用者的電腦上。每次在同一台電腦請求一個網頁的瀏覽器,它將把餅乾太多。與ASP ,你都可以創造和檢索的cookie值。


-------------------------------------------------- ------------------------------

如何建立一個Cookie ?
該“ Response.Cookies ”命令用於建立的Cookie 。

註: Response.Cookies命令必須出庭<html>標記。

在下面的例子中,我們將建立一個cookie命名為“名字” ,並指定值“亞曆克斯” ,以它:

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

也有可能的cookie ,如確定一個日期時,應到期的cookie :

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

 

如何擷取一個cookie價值嗎?
該“ Request.Cookies ”命令用於檢索一個cookie值。

在下面的例子中,我們擷取的價值Cookie的名為“名字” ,並顯示在網頁上:

 

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

 

輸出結果.

 

Firstname=Alex

 

一個Cookie的鑰匙
如果一個cookie包含了收集的多重價值,我們說的cookie了鑰匙。

在下面的例子中,我們將建立一個Cookie收集命名為“使用者” 。 “使用者的” 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"%>
 
        

假設您的伺服器已寄出所有的cookies以上的使用者。

現在,我們要閱讀所有的Cookie發送給使用者。下面的例子顯示了如何做到這一點

(請注意,下面的代碼檢查是否有一個Cookie鍵與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=Smith
user:country=Norway
user:age=25

 

如果瀏覽器不支援cookies ?
如果您的申請涉及的瀏覽器不支援cookies ,您將不得不使用其他方法來傳遞資訊從一個網頁到另一個在您的申請。有兩種方法這樣做的:

1 。新增參數以一個網址
您可以添加參數的網址

 

<a href="welcome.asp?fname=John&lname=Smith">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>")%>
轉載請註明來自www.111cn.net/asp/asp.html
 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.