6-8:Session 物件:在使用者層級的資訊保存

     
Application 物件可讓同一個 Web 應用程式共用資訊,而 Session 物件可讓同一個使用者在不同的 Web 應用程式中共用資訊。換句話說,同一個使用者在不同的 Request 中,可用 Session 物件來保存資訊,而其保存資訊的方法,則是靠 Cookies 來達成。因此,要能夠使用 Session 物件的首要條件,就是用戶端的 Cookies 功能必須是開啟的。

Session 物件提供四種性質(Properties)、一種方法(Method)、兩個事件(Events)與兩個集合(Collections),列表如下:

Session 物件的性質
性質說明
CodePageCodePage的識別碼
LCIDLCID的識別碼
SessionID用戶端的SessionID
TimeoutSession 物件的有效時間(以分鐘為單位),預設為20分鐘

Session 物件提供的方法
方法說明
Abandon刪除 Session 物件所含的所有資訊
Contents.Remove(item or index)刪除 Contents 集合中的某一個項目
Contents.RemoveAll刪除 Contents 集合中的所有項目

Session 物件提供的事件
事件說明
OnStart啟動一個 Session 物件時所觸發的函式,此函式必須放在 global.asa 檔案
OnEnd結束一個 Session 物件時所觸發的函式,此函式必須放在 global.asa 檔案

Session 物件提供的集合
集合說明
Contents所有加在 Session 物件的變數集合
StaticObjects所有在使用者層級(Session Scope)所宣告的物件集合

當使用者點選某一個網頁時,伺服器就會對此使用者分配一個 session ID,並以 cookie 的方式記錄在用戶端。此 session ID 的有效期間是 20 分鐘,這些資訊都可由下列網頁來呈現:

在上述範例中,只要使用者在20分鐘內連結到相同的網頁,都會得到相同的 session ID,上述範例的完整原始檔案 (session/sessionid.asp) 如下:

<%@language=JScript%>
<%title="顯示 Session.SessionID 及 Session.Timeout "%>
<!--#include file="../head.inc"-->
<hr>

<p>
Your session ID is <b><font color=green><%=Session.SessionID%></font></b>, which will be changed in
<b><font color=green><%=Session.Timeout%></font></b> minutes when you access this page again.
<p>
All session info:
	<ul>
	<li>Session.SessionID = <font color=green><%=Session.SessionID%></font>
	<li>Session.Timeout = <font color=green><%=Session.Timeout%></font>
	<li>Session.CodePage = <font color=green><%=Session.CodePage%></font>
	<li>Session.LCID = <font color=green><%=Session.LCID%></font>
	</ul>
<hr>
<!--#include file="../foot.inc"-->

Session 和 Application 物件一樣,都有 OnStart 和 OnEnd 兩個事件,這兩個事件對應的函式是 Session_OnStart() 和 Session_OnEnd(),也都必須存放在 global.asa 檔案中。若 Application 和 Session 同時啟動,ASP 會先執行 Application_OnStart(),再執行 Session_OnStart()。若兩者同時結束,ASP 會先執行 Session_OnEnd(),再執行 Application_OnEnd()。這些執行順序可列出如下:

  1. Application_OnStart()
  2. Session_OnStart()
  3. ASP scripts
  4. Session_OnEnd()
  5. Application_OnEnd()

以下我們將使用「加強版的訪客計數器」,來說明如何使用 Session 及 Application 物件,使得用戶端在點選「重新整理」時,記數器的值不會一再累加。

其方法可說明如下:在被計數的網頁 ASP 程式碼中,檢查 Session("PreviouslyOnLine") 的值,若是 true,則不做任何事。若是 false,則將其值改為 true,並將 Application("Counter") 的值加一。換句話說,只要每次有人使用這瀏覽此網頁,而且 Session("PreviouslyOnLine") 的值是 false,Application("Counter") 的值就會加一,其值即代表此網頁被點選的次數,它並不會因為使用者點選「重新整理」而增加。請見以下範例:

上述範例的完整原始檔案 (session/pagehit1.asp) 如下:

<%@language=JScript%>
<%title="使用 Application 與 Session 物件來防止計數資料的竄改:方法一"%>
<!--#include file="../head.inc"-->
<hr>

<%
if (Session("PreviouslyOnLine")!=true){
	Application("Counter")++;
	Session("PreviouslyOnLine") = true;
}
%>
<h3 align=center>您是第 <font color=red><%=Application("Counter")%></font> 位貴賓!</h3>

<hr>
<!--#include file="../foot.inc"-->
此外,我們可以再加上「獨立計數」、「計數器啟用時間」等功能,得到一個較完整的範例:

上述範例的完整原始檔案 (session/pagehit2.asp) 如下:

<%@language=JScript%>
<%title="使用 Application 與 Session 物件來防止計數資料的竄改:方法二"%>
<!--#include file="../head.inc"-->
<hr>

<%
URL = Request.ServerVariables("URL");
now = new Date();
if (Application(URL)==null)
	Application(URL&"StartTime") = now.toLocaleString();
if (Session(URL)==null){
	Application(URL)++;
	Session(URL)="junk";
} else {%>
	<script>
	alert("你想竄改記數器?沒那麼容易喔!");
	</script>
<%}%>
<h3 align=center>從 <font color=green><%=Application(URL&"StartTime")%></font> 以來,您是第 <font color=red><%=Application(URL)%></font> 位貴賓!</h3>

<hr>
<!--#include file="../foot.inc"-->
上述記數器範例雖然有許多功能,但仍有一個缺點:伺服器重開機時,Application 物件會被清除,因此所有的計數資料就不見了,一切歸零。要解決這個問題,則要將計數資料寫入檔案,後面再詳細介紹。

Session 物件是一個很好用的保存資訊方法,其他相關應用有:

特別必須注意的是:Session 物件的正常運作必須倚賴用戶端開啟 Cookies 功能,因此如果妳的網頁使用到 Session 物件,最好必須在網頁之前加上測試部分,例如:

<%
If Not IsCookiesOn Then
	Response.Write("Please turn on your Cookies function before viewing this page!")
	Response.End
End If
%>
Original contents goes here ...
在上例中,IsCookiesOn 並不是一個內建的函數,而是由我們自己定義的函數。(那位同學能幫我寫出這個函數?謝謝!)

範例整理

參考資料


回到「第 6 章:ASP