明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

asp+ 設(shè)置Cookie 方法大全

[摘要]/*豆腐制作 都是精品www.asp888.net 豆腐技術(shù)站如果轉(zhuǎn)載 請(qǐng)?zhí)砑舆@個(gè) 版權(quán)信息*/<%@ Page Language="VB" %><script...
/*
豆腐制作 都是精品
www.asp888.net 豆腐技術(shù)站
如果轉(zhuǎn)載 請(qǐng)?zhí)砑舆@個(gè) 版權(quán)信息
*/
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Const COOKIE_NAME As String = "豆腐站點(diǎn)"
Const COOKIE_VALUE As String = "http://www.asp888.net"
' 聲明 Cookie 對(duì)象
Dim objCookieObject As HttpCookie
Sub btnSetCookie_OnClick(Sender As Object, E As EventArgs)
' Create a cookie object - I'm passing name and value,
' but you can also pass in a name and set the value later.

objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE)

' 另外的一種操作Cookie 的 方法
'objCookieObject = New HttpCookie(COOKIE_NAME)
'objCookieObject.Name = COOKIE_NAME
'objCookieObject.Value = "sdsd"

' 設(shè)置Cookie 的 過(guò)期時(shí)間 2001/12/31 23:59:59
objCookieObject.Expires = New DateTime(2001, 12, 11, 23, 59, 59)

' 下面的這些可以不使用
objCookieObject.Domain = "www.asp888.net"
objCookieObject.Path = "/path/"
objCookieObject.Secure = True

Response.AppendCookie(objCookieObject)
End Sub

Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs)
objCookieObject = New HttpCookie(COOKIE_NAME)
' 刪除Cookie
objCookieObject.Expires = New DateTime(1974, 11, 12)
Response.AppendCookie(objCookieObject)
End Sub

Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs)
objCookieObject = Request.Cookies(COOKIE_NAME)
If Not(objCookieObject = null) Then
lblCookieDetails.Text = objCookieObject.Name
lblCookieDetailsName.Text = objCookieObject.Name
lblCookieDetailsValue.Text = objCookieObject.Value
lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString
lblCookieDetailsDomain.Text = objCookieObject.Domain
lblCookieDetailsPath.Text = objCookieObject.Path
lblCookieDetailsSecure.Text = objCookieObject.Secure.ToString
lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString
Else
lblCookieDetails.Text = "Cookie Not Set!"
lblCookieDetailsName.Text = ""
lblCookieDetailsValue.Text = ""
lblCookieDetailsExpires.Text = ""
lblCookieDetailsDomain.Text = ""
lblCookieDetailsPath.Text = ""
lblCookieDetailsSecure.Text = ""
lblCookieDetailsHasKeys.Text = ""
End If
End Sub
</script>

<html>
<head>
<title> asp+ 操作Cookie 方法大全 </title>
</head>
<body>

<h4>我們?cè)O(shè)置的Cookie 的名稱是: <em><%= COOKIE_NAME %></em></h4>

<form method="post" runat="server">
<asp:Button type="submit" id="btnSetCookie" text="Set Cookie" OnClick="btnSetCookie_OnClick" runat="server" />
<asp:Button type="submit" id="btnRemoveCookie" text="Remove Cookie" OnClick="btnRemoveCookie_OnClick" runat="server" />

<p>
想要看看 Cookie 的當(dāng)前的各個(gè)屬性,請(qǐng)點(diǎn)擊下面的按鈕
</p>

<asp:Button type="submit" id="btnGetCookie" text="Get Cookie Details" OnClick="btnGetCookie_OnClick" runat="server" />
</form>

<p>
<strong>Details of:</strong> <asp:label id="lblCookieDetails" runat="server" />
</p>

<table border="1">
<thead>
<tr>
<th>Cookie屬性</th>
<th>Cookie 的屬性Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>名稱</td>
<td><asp:label id="lblCookieDetailsName" runat="server" /></td>
</tr>
<tr>
<td>Cookie的值</td>
<td><asp:label id="lblCookieDetailsValue" runat="server" /></td>
</tr>
<tr>
<td>過(guò)期時(shí)間</td>
<td><asp:label id="lblCookieDetailsExpires" runat="server" /></td>
</tr>
<tr>
<td>所在域</td>
<td><asp:label id="lblCookieDetailsDomain" runat="server" /></td>
</tr>
<tr>
<td>路徑</td>
<td><asp:label id="lblCookieDetailsPath" runat="server" /></td>
</tr>
<tr>
<td>安全性</td>
<td><asp:label id="lblCookieDetailsSecure" runat="server" /></td>
</tr>
<tr>
<td>主鍵</td>
<td><asp:label id="lblCookieDetailsHasKeys" runat="server" /></td>
</tr>
</tbody>
</table>

</body>
</html>


作者:豆腐()