2025-04-27 07:49:33 -04:00

123 lines
6.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<TITLE>Session_OnStart</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
TempString = navigator.appVersion
if (navigator.appName == "Microsoft Internet Explorer"){
// Check to see if browser is Microsoft
if (TempString.indexOf ("4.") >= 0){
// Check to see if it is IE 4
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else {
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
}
}
else if (navigator.appName == "Netscape") {
// Check to see if browser is Netscape
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
//-->
</script>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Window_OnLoad()
Dim frmContents
On Error Resume Next
If Not Parent Is Nothing Then
Set frmContents = Parent.Contents
If Not frmContents Is Nothing Then
frmContents.Window.TOCSynch_Click
End If
End If
End Sub
//--></SCRIPT><META NAME="DESCRIPTION" CONTENT="Internet Information Services reference information">
<META HTTP-EQUIV="PICS-Label" CONTENT='(PICS-1.1 "<http://www.rsac.org/ratingsv01.html>" l comment "RSACi North America Server" by "inet@microsoft.com <mailto:inet@microsoft.com>" r (n 0 s 0 v 0 l 0))'>
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="MS-IT-LOC" Content="Internet Information Services">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H3><A NAME="_session_onstart"></A><SUP></SUP>Session_OnStart</H3>
<P>The <B>Session_OnStart </B>event occurs when the server creates a new session. The server processes this script prior to executing the requested page. The <B>Session_OnStart</B> 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 (<A HREF="/iishelp/iis/htm/asp/vbob8zw4.htm">Application</A>, <A HREF="/iishelp/iis/htm/asp/vbob554k.htm">ObjectContext</A>, <A HREF="/iishelp/iis/htm/asp/vbob5ulw.htm">Request</A>, <A HREF="/iishelp/iis/htm/asp/vbob5sj8.htm">Response</A>, <A HREF="/iishelp/iis/htm/asp/vbob7838.htm">Server</A>, and <A HREF="/iishelp/iis/htm/asp/vbob12d0.htm">Session</A>) are available and can be referenced in the <B>Session_OnStart </B>event script. </P>
<H6>Syntax</H6>
<P><B>&lt;SCRIPT LANGUAGE=</B><I>ScriptLanguage</I> <B>RUNAT=Server&gt; </B><BR>
<B>Sub Session_OnStart</B><BR>
. . .<BR>
<B>End Sub</B></P>
<P><B>&nbsp;</B></P>
<P><B>&lt;/SCRIPT&gt;</B></P>
<P><B>&nbsp;</B></P>
<H6>Parameters</H6>
<DL>
<DT><I>ScriptLanguage</I></DT>
<DD>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 &lt;SCRIPT&gt; tags.</DD>
</DL>
<H6>Example</H6>
<P>Although the <B>Session </B>object persists if the <B>Session_OnStart </B>event contains a call to the <A HREF="/iishelp/iis/htm/asp/vbob8h10.htm">Redirect</A> or <A HREF="/iishelp/iis/htm/asp/vbob2sis.htm">End</A> methods, the server stops processing the script in both the Global.asa file and in the file that triggered the <B>Session_OnStart </B>event. </P>
<P>You can call the <A HREF="/iishelp/iis/htm/asp/vbob8h10.htm">Redirect</A> method in the <B>Session_OnStart</B> 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 <B>Session_OnStart </B>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 <B>Response.Redirect</B> method. This is demonstrated in the following example.</P>
<PRE><CODE>&lt;SCRIPT RUNAT=Server LANGUAGE=VBScript&gt;
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
&lt;/SCRIPT&gt;
</CODE></PRE>
<P>The preceding example only works for browsers that support cookies. Because a noncookie browser does not return the <B>SessionID</B> cookie, the server creates a new session each time the user requests a page. Thus for each request, the server processes the <B>Session_OnStart</B> 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.</P>
<H6>Remarks</H6>
<P>You should note that any <B>Session_OnStart</B> event script that follows a call to the <A HREF="/iishelp/iis/htm/asp/vbob8h10.htm">Redirect</A> method is not executed. For this reason, you should call the <B>Redirect</B> method last in your event script. This is demonstrated in the following example. </P>
<PRE><CODE>&lt;SCRIPT LANGUAGE=VBScript RUNAT=Server&gt;
Sub Session_OnStart
'Session initialization script.
Response.Redirect "http:/server/app/StartHere.asp"
End sub
&lt;/SCRIPT&gt;
</CODE></PRE>
<P>In the preceding example the <A HREF="/iishelp/iis/htm/asp/vbob8h10.htm">Redirect</A> method hides any text displayed to the client during the session-initialization script. </P>
<H6>See Also</H6>
<P><A HREF="/iishelp/iis/htm/asp/glob2y90.htm">Session_OnEnd</A>, <A HREF="/iishelp/iis/htm/asp/glob4f78.htm">Application_OnStart</A></P>
<hr class="iis" size="1">
<p align="center"><em><a href="../../../common/colegal.htm">&copy; 1997-2001 Microsoft Corporation. All rights reserved.</a></em></p>
</BODY>
</HTML>