ASP.NET清空session 关于Session.Abandon的使用注意事项

 

本文关于session对象方法Abandon,使用该方法可以清空session中的值。

首先阅读一下微软的文档:

Remarks
When the Abandon method is called, the current Session object is queued for deletion, but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to Abandon, but not in any subsequent Web pages.

/>

如果调用Abandon 方法的话,在当前页面的脚本执行完之后,当前的session对象将会被删除;在本页中仍然可以调用session对象中存储的值

/>


For example, in the following script, the third line prints the value Mary. This is because the Session object is not destroyed until the server has finished processing the script.

<%
Session.Abandon();
Session("MyName") = "Mary"
Reponse.Write(Session("MyName"))
%>

If you access the variable MyName on a subsequent Web page, it is empty. This is because MyName was destroyed with previous Session object when the page containing the above example finished processing.

The server creates a new Session object when you open a subsequent Web page after abandoning a session. You can store variables and objects in this new Session object.

/>

被删除的session在后面的页面中取值的话,将得到一个空的结果;使用abandon方法后,你可以创建新的session变量

/>

 

Session.Abandon是将服务器上该网站(例如www.aspxuexi.com的所有在线访问者)的所有session都删除.session是会话级别的变量,即使不调用Abandon方法,在会话超时后,服务器上保留的值仍然会被清空,所有web编程脚本都会有这个问题;建议使用cookies保留长期的会话数据。会话超时后(例如长时间未刷新),标记访问的session SessionID将重新产生,服务器会认为这是一次全新的访问。Sessionid是完全随机的,一个session变量大约会占用10k的内存。(另外一个说法是4k)。需要了解的是,不管程序是否使用session,SessionID总是存在的,而且会以cookie的方式发送到浏览器。

如果网站配备Global.asax(从来没见过现在2007年还有人使用这个东西),Session.Abandon()将激活session_onend事件  。by/www.aspxuexi.com

单一用户退出登陆时尽量用清空的方法,而不要用Session.Abandon()这删除的方法.
如:session("userid") = ""
直接将session("userid")变量置空即可`~`

或者使用Session.Contents.Remove("UserName")来删除session("UserName")的变量引用

也可用Session.Contents.RemoveAll来删除所有的Session的变量引用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值