Session_OnStart

The Session_OnStart event occurs when the server creates a new session. The server processes this script prior to executing the requested page. The Session_OnStart event is a good time for you to set any session-wide variables, because they will be set before any pages are accessed. All the built-in objects (Application, ObjectContext, Request, Response, Server, and Session) are available and can be referenced in the Session_OnStart event script.

Syntax

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

Parameters

ScriptLanguage
Specifies the scripting language used to write the event script. It may 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.

Example

Although the Session object persists if the Session_OnStart event contains a call to the Redirect or 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 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 enters 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. This is demonstrated 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 compare, 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>

The preceding example only works for browsers that support cookies. Because a noncookie browser 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 script below, it is recommended that you put a notice on your starting page to inform users that the site requires a cookie-enabled browser.

Remarks

You should note that any Session_OnStart event script that follows a call to the Redirect method is not executed. For this reason, you should call the Redirect method last in your event script. This is demonstrated 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 Redirect method hides any text displayed to the client during the session-initialization script.

See Also

Session_OnEnd, Application_OnStart