程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 在ASP+中使用Cookie

在ASP+中使用Cookie

編輯:ASP技巧
<%@ Page Language="VB" %>

<script language="VB" runat="server">
Const COOKIE_NAME  As String = "test-cookIE-name"
Const COOKIE_VALUE As String = "test-cookIE-value"

' Declare our cookIE object
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.
' ie. objCookieObject = New HttpCookie(COOKIE_NAME)
objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE)

' We already set these above!
'objCookieObject.Name   = COOKIE_NAME
'objCookieObject.Value  = COOKIE_VALUE

' Additional cookIE PRopertIEs:
objCookIEObject.Expires = New DateTime(2001, 12, 31, 23, 59, 59)

' Normally you can leave these alone.
' The defaults will work fine for most uses.
'objCookIEObject.Domain  = "www.domain.com"
'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)

' Expire it on the day I was born just so we're sure it's a date in the past.
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

' I'm ignoring collections.  They're outside the realm of this basic sample.
' FYI: Additional properties related to cookIE collections: Values, Item
End Sub
</script>

<Html>
<body>

<h4>The cookie name we're using for this sample is: <em><%= COOKIE_NAME %></em></h4>

<form action="cookIEs.ASPx" 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>
To see the cookIE's current status you'll need to click below.  This is because the response
which adds or deletes the cookIE happens after the request is already done.  As such, those changes aren't
available from the request collection until the next request.
</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>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td><ASP:label id="lblCookIEDetailsName" runat="server" /></td>
</tr>
<tr>
<td>Value</td>
<td><ASP:label id="lblCookIEDetailsValue" runat="server" /></td>
</tr>
<tr>
<td>Expires</td>
<td><ASP:label id="lblCookIEDetailsExpires" runat="server" /></td>
</tr>
<tr>
<td>Domain</td>
<td><ASP:label id="lblCookIEDetailsDomain" runat="server" /></td>
</tr>
<tr>
<td>Path</td>
<td><ASP:label id="lblCookIEDetailsPath" runat="server" /></td>
</tr>
<tr>
<td>Secure</td>
<td><ASP:label id="lblCookIEDetailsSecure" runat="server" /></td>
</tr>
<tr>
<td>Has Keys</td>
<td><ASP:label id="lblCookIEDetailsHasKeys" runat="server" /></td>
</tr>
</tbody>
</table>

</body>
</Html>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved