內存是臨時存儲,關機後信息就沒了。硬盤是長久存儲,不刪除就一直在。內存容量決定你電腦能同時運行的程序多少,硬盤容量決定你電腦能存放多少東西。javascript
比如內存是手,硬盤是口袋,東西能夠臨時放在手裏,但是口袋纔是東西的最終存放地方。html
1:設置過時時間失效(只要設置了過時時間cookie就會存儲在硬盤裏面)java
2:當會話結束時失效,即關閉瀏覽器窗口(若是沒有設置Expires,cookie就會存儲在內存裏面)瀏覽器
手動刪除cookie失效(這個不知你們算不算)cookie
1 <%@ Page Language="C#"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head runat="server"> 5 <title>內存cookie和硬盤cookie</title> 6 <script type="text/javascript" runat=server> 7 // 內存 8 protected void btn_cookie_1_Click(object sender, EventArgs e) 9 { 10 HttpCookie cookie = new HttpCookie("Memory"); 11 cookie.Value = "內存存儲"; 12 HttpContext.Current.Response.AppendCookie(cookie); 13 } 14 // 硬盤 15 protected void btn_cookie_2_Click(object sender, EventArgs e) 16 { 17 HttpCookie cookie = new HttpCookie("Hard disk"); 18 cookie.Value = "硬盤存儲"; 19 cookie.Expires = System.DateTime.Now.AddDays(1); 20 HttpContext.Current.Response.AppendCookie(cookie); 21 } 22 // 23 </script> 24 </head> 25 <body> 26 <form id="form1" runat="server"> 27 <div> 28 <asp:Button ID="btn_cookie_1" runat="server" Text="內存cookie" 29 onclick="btn_cookie_1_Click" /> 30 <asp:Button ID="btn_cookie_2" runat="server" Text="硬盤cookie" 31 onclick="btn_cookie_2_Click" /> 32 </div> 33 </form> 34 </body> 35 </html>