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对象中存储的值,by www.aspxuexi.com。

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.


+展开
-VBScript
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.asa(从来没见过现在2007年还有人使用这个东西),Session.Abandon将激活session_onend事件 。
  单一用户退出登陆时尽量用清空的方法,而不要用Session.Abandon这删除的方法.
如:
+展开
-VBScript
session("userid") = ""

直接将session("userid")变量置空即可`~`

  或者使用
+展开
-VBScript
Session.Contents.Remove("UserName")

来删除session("UserName")的变量引用。

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

加支付宝好友偷能量挖...


评论(0)网络
阅读(189)喜欢(0)Asp.Net/C#/WCF