在ASP+中使用Cookie-虚拟主机资讯
<%@ 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 - im 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 = "http://www.zjjv.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 were sure its 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
im ignoring collections. theyre 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 were 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 cookies current status youll 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 arent
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>
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://http://www.zjjv.com//
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!