ASP學(xué)習(xí)之Cookie的基本使用技巧
作者: 來(lái)源: 發(fā)布時(shí)間:2011-6-10 15:21:18 點(diǎn)擊:
< %@ LANGUAGE="VBSCRIPT" % >
< %
RequestName = Request.Form("Name")
RequestLeaveMeAlone = Request.Form("LeaveMeAlone")
If RequestName < >"" or RequestLeaveMeAlone < >"" then
Response.Cookies("MySiteVisitorName") = RequestName
Response.Cookies("MySiteVisitorName").Expires = #January 01, 2010#
Response.Cookies("MySiteLeaveMeAlone") = RequestLeaveMeAlone
Response.Cookies("MySiteLeaveMeAlone").Expires = #January 01, 2010#
End if
接著,讀取cookie:
VisitorName = request.cookies("MySiteVisitorName")
LeaveMeAlone = request.cookies("MySiteLeaveMeAlone")
如果cookie在訪問(wèn)者的計(jì)算機(jī)上不存在,就創(chuàng)建一個(gè)表單,詢問(wèn)相關(guān)信息:
If VisitorName ="" and LeaveMeAlone ="" then
% >
< HTML >
< HEAD >
< /HEAD >
< body bgcolor="#ccffff" text="black" link="navy" vlink="purple" >
< DIV ALIGN="CENTER" >
< form action="index.ASP" method="POST" >
< H2 >Let's be friends< /H2 >
What's your name (leave blank and hit the Submit button if you don't want us to know)?
< input type="text" name="name" >< br >< br >
< input type="hidden" name="LeaveMeAlone" value="x" >
< input type="submit" value="Submit" >
< /FORM >
< /DIV >
< /BODY >
< %
End if
如果cookie已經(jīng)存在,并且用戶名字存在,就顯示給訪問(wèn)者一個(gè)歡迎界面,然后執(zhí)行其余的代碼。
If VisitorName < > "" then
Response.write "Hi, " & VisitorName & "! I hope you are
having a great day!"
End if
'rest of the page
% >
盡管上面的這個(gè)例子很簡(jiǎn)單,但可以從中擴(kuò)展許多富有創(chuàng)造力的應(yīng)用。你可以在表單中加入許多功能,以便定制化Web站點(diǎn)。還可以讓訪問(wèn)者定制網(wǎng)站的色彩、字體,以至于其他web元素。有可能的話,你可以詢問(wèn)訪問(wèn)者的生日,當(dāng)訪問(wèn)者在那一天來(lái)訪時(shí),你就可以顯示“生日快樂(lè)”的信息給他。 如你所見(jiàn),cookie的擴(kuò)展性是無(wú)窮的,這篇文章僅僅是拋磚引玉。
< %
RequestName = Request.Form("Name")
RequestLeaveMeAlone = Request.Form("LeaveMeAlone")
If RequestName < >"" or RequestLeaveMeAlone < >"" then
Response.Cookies("MySiteVisitorName") = RequestName
Response.Cookies("MySiteVisitorName").Expires = #January 01, 2010#
Response.Cookies("MySiteLeaveMeAlone") = RequestLeaveMeAlone
Response.Cookies("MySiteLeaveMeAlone").Expires = #January 01, 2010#
End if
接著,讀取cookie:
VisitorName = request.cookies("MySiteVisitorName")
LeaveMeAlone = request.cookies("MySiteLeaveMeAlone")
如果cookie在訪問(wèn)者的計(jì)算機(jī)上不存在,就創(chuàng)建一個(gè)表單,詢問(wèn)相關(guān)信息:
If VisitorName ="" and LeaveMeAlone ="" then
% >
< HTML >
< HEAD >
< /HEAD >
< body bgcolor="#ccffff" text="black" link="navy" vlink="purple" >
< DIV ALIGN="CENTER" >
< form action="index.ASP" method="POST" >
< H2 >Let's be friends< /H2 >
What's your name (leave blank and hit the Submit button if you don't want us to know)?
< input type="text" name="name" >< br >< br >
< input type="hidden" name="LeaveMeAlone" value="x" >
< input type="submit" value="Submit" >
< /FORM >
< /DIV >
< /BODY >
< %
End if
如果cookie已經(jīng)存在,并且用戶名字存在,就顯示給訪問(wèn)者一個(gè)歡迎界面,然后執(zhí)行其余的代碼。
If VisitorName < > "" then
Response.write "Hi, " & VisitorName & "! I hope you are
having a great day!"
End if
'rest of the page
% >
盡管上面的這個(gè)例子很簡(jiǎn)單,但可以從中擴(kuò)展許多富有創(chuàng)造力的應(yīng)用。你可以在表單中加入許多功能,以便定制化Web站點(diǎn)。還可以讓訪問(wèn)者定制網(wǎng)站的色彩、字體,以至于其他web元素。有可能的話,你可以詢問(wèn)訪問(wèn)者的生日,當(dāng)訪問(wèn)者在那一天來(lái)訪時(shí),你就可以顯示“生日快樂(lè)”的信息給他。 如你所見(jiàn),cookie的擴(kuò)展性是無(wú)窮的,這篇文章僅僅是拋磚引玉。
上一篇:查看asp源碼的技巧分享 下一篇: 在Asp中如何建立使用存儲(chǔ)過(guò)程
[收藏此文章]