Partager via


Session_OnStart Event

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.

<SCRIPT LANGUAGE=ScriptLanguage RUNAT=Server>
Sub Session_OnStart
. . . 
End Sub
</SCRIPT>

Parameters

  • ScriptLanguage
    Specifies the scripting language used to write the event script. It can be any supported scripting language, such as VBScript or JScript. If more than one event uses the same scripting language, they can be combined under a single set of <SCRIPT> tags.

Remarks

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= &quot;VBScript&quot; RUNAT=Server>
 Sub Session_OnStart
  'Session initialization script.
  Response.Redirect "https://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.

Example Code

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.

Note

This example only works for browsers that support cookies. Because a browser that does not support cookies does not return the SessionID cookie, the server creates a new session each time the user requests a page. Thus, for each request, the server processes the Session_OnStart script and redirects the user to the starting page. If you use the following script, it is recommended that you put a notice on your starting page to inform users that the site requires a browser that is enabled to use cookies.

<SCRIPT RUNAT=Server Language= &quot;VBScript&quot;>
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>

Requirements

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.

Product: IIS

See Also