帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
最新的关于Cookies的操作
作者: 发布时间:2005-03-12 来源:
So anyways, on to the code. The first thing we need to think of is exactly what data will need to be
stored in the cookie. In this case we will use info submitted by the user via this form:

 

<html>
<body>
  <form action="cookie.asp" method="post">
       Name:
       <input type="text" name="name" size="20">
       <input type="submit" value="submit">
  </form>
</body>
</html>


Now let's get to the fun part, setting the cookie info. You can use the following code to set the cookie
value on the clients machine:

 

<%
Response.buffer = True
' first we put the data from the form into a variable
name = Request.Form("name")

'then we can set the cookie value using this:
Response.Cookies("name") = name
%>

Ok, so now the cookie is set and we need to retrieve the info. You can use this to retrive the cookie
data:

 

<%
' first we retrive the data and set it to a variable
name = Request.Cookies("name")

' then we post it to the website
Response.Write (name)
%>

And it's that easy. But wait, there is more. There are a few properties that you can set using cookies.

The first property is the Domain property. This is the domain that the cookie originated from. The cookie
can only be read by the domain it originated from. It is set by default to the domain in which it was
created, but you can alter it for your needs. You can set it using this:

 

Response.Cookies("name").Domain = "www.cookiemonster.com"

The next important property is the Expires property. This specifies the date the cookie should expire. If
it is set to a past date then it will expire when the browser is closed.

When setting the date it can be set a couple of ways. You can use the current date and add or subtract
days like so:

 

Response.Cookies("name").Expires = Date + 365

Or you can set it to a specific date like this:

 

Response.Cookies("name").Expires = #January 01, 1999#

The Path property is the next important property when using cookies. This specifies in more detail the
exact path on the domain that can use the cookie. For example, this would set the path that can retrieve
the cookie info:

 

Response.Cookies("name").Path = "/this/is/the/path"

The last property of the cookies object is the Secure property. If set, the cookie will only be set if the
browser is using secure sockets or https:// to connect. You can set it like so:

 

Response.Cookies("name").Secure = True

* as a note, this doesn't mean the cookie is secure. It's just like every other cookie, just a text file.

Using a dictionary cookie
What is a dictionary cookie? Basically it's just a cookie that can h old several values. For instance lets
say you wanted to gather the users first and last name and store them in one cookie.

You could use this:

 

Response.Cookies("name")("first") = "John"
Response.Cookies("name")("last") = "Smith"

This gives you the option of storing all your neccesary info in one cookie. There are limits on how much
data you can put on a clients browser. Most browsers allow you to place 20 cookies per domain at a maximum
of 4k each. For more info about what cookies can and can't do, visit www.cookiecentral.com.

So that basically wraps it up. Good luck in your cookie escapades and if you have any comments or
questions feel free to contact me. 
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·最新JAVA手机群聊软件MSG横空出世  (2005-03-12)
 ·最新产品"帝国备份王"正在开发  (2004-10-23)
 ·最新消息:帝国新闻系统2.0,在线  (2004-08-01)
 ·最新开放论坛-手把手学jsp/java  (2003-04-25)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统