帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
关于释放session的两篇文章(二)
作者:东方蜘蛛(转) 发布时间:2005-03-12 来源:chinaasp.com
Deleting a Subset of Session Variables

When using Sessions to store variables in, I use a naming convention - for example, for all Customer related info I prefix the session variable with the substring Customer. (so for the CustomerID it would be Customer.ID, the customer username would be Customer.Name, etc.) This is very useful when viewing the session objects as you can see the related objects straight off.

The problem I had the other day was that I wanted to remove only those items from the Session which were prefixed SW.. So first off I used the following code:

'---------------------------------------
Session("SW.1")="Test 1"
Session("SW.2")="Test 2"
Session("Other")="Other"

For Each SessionItem in Session.Contents
If Left(SessionItem,3) = "SW." then
Session.Contents.Remove(SessionItem)
end if
Next
'---------------------------------------

This seems fine, but when it's run, what happens is that SW.1 is removed, but SW.2 is NOT removed. Why? I'm not exactly sure, but I guess that the index is then reset so that SW.2 is now where SW.1 was, and seeing as we have iterated past that item in the For Each...Next statement, the loop just moves to Other, missing out SW.2 altogether! Eek!

So to get round this I wrote the following function, which will properly delete all Session variables that begin with a specified substring:

'---------------------------------------
function SessionRemoveSelected(sItemPrefix)
'/////////////////////////////////////////////////
' Remove Selected Items starting with sItemPrefix
' from the Session. e.g. SS. will remove SS.ID and
' SS.NAME but not CustomerID Returns True or False
' depending on whether any items where removed.
'---------------------------------------
' sItemPrefix [string] : Item Prefix
'/////////////////////////////////////////////////
dim arySession()
dim lCount, lPrefixLength
dim SessionItem
dim blnResult

lCount = -1
lPrefixLength = len(sItemPrefix)
blnResult = false

' temporarily store in array items to remove
For Each SessionItem in Session.Contents
if left(SessionItem,lPrefixLength) = sItemPrefix then
lCount = lCount + 1
redim preserve arySession(lCount)
arySession(lCount) = SessionItem
end if
Next

' remove items
if IsArray(arySession)and lCount >= 0 then
for lCount = LBound(arySession) to UBound(arySession)
Session.Contents.Remove(arySession(lCount))
next
blnResult=true
end if

SessionRemoveSelected = blnResult
end function
'-------------------------------------------------

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·关于释放session的两篇文章(一)  (2005-03-12)
 ·关于用ADO STREAM做的无组件上传  (2005-03-12)
 ·关于存储过程分页  (2005-03-12)
 ·关于分页办法  (2005-03-12)
 ·一次关于ASP变量和对像关闭与不关  (2005-03-12)
 ·关于4005错误谈我的一点经验  (2005-03-12)
 ·关于密码校验  (2005-03-12)
 ·关于页面和代码分离的  (2005-03-12)
 ·关于INDEX SERVER+ASP建立查询引  (2005-03-12)
 ·关于页面缓存清除的方法小结(整  (2005-03-12)

   栏目导行
  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个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统