The Session_OnStart event occurs when the server creates a new session. The server processes this script before executing the requested page. It is a good practice to set any session-wide variables with the Session_OnStart event because the variables would be set before any pages are accessed. All the built-in objects (Application Object, ObjectContext Object, Request Object, Response Object, Server Object, and Session Object) are available and can be referenced in the Session_OnStart event script.
You should note that any Session_OnStart event script that follows a call to the Response.Redirect method is not executed. For this reason, you should call the Redirect method last in your event script, as shown in the following example.
<SCRIPT Language= "VBScript" RUNAT=Server> Sub Session_OnStart 'Session initialization script. Response.Redirect "http://server/app/StartHere.asp" End sub </SCRIPT>
In the preceding example the Response.Redirect method hides any text displayed to the client during the session-initialization script.
Although the Session object persists if the Session_OnStart event contains a call to the Response.Redirect or Response.End methods, the server stops processing the script in both the Global.asa file and in the file that triggered the Session_OnStart event.
You can call the Response.Redirect method in the Session_OnStart event, for example, to ensure that users always start a session at a particular Web page. When the user initially opens the application, the server creates a session for that user and processes the Session_OnStart event script. You can include script in this event to check whether the page opened by the user is the starting page and, if not, direct the user to the starting page by calling the Response.Redirect method, as shown in the following example.
<SCRIPT RUNAT=Server Language= "VBScript"> Sub Session_OnStart 'Make sure that new users start on the correct 'page of the ASP application. 'Replace the value given to startPage below 'with the virtual path to your application's 'start page. startPage = "/MyApp/StartHere.asp" currentPage = Request.ServerVariables("SCRIPT_NAME") 'Do a case-insensitive comparison, and if they 'don't match, send the user to the start page. If strcomp(currentPage,startPage,1) then Response.Redirect(startPage) End If End Sub </SCRIPT>
Client: Requires Windows XP Professional, Windows 2000 Professional, or Windows NT Workstation 4.0.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0.