⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asp_sessions.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>ASP Session object</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>

<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

<h1>ASP Session Object</h1>
<a href="asp_cookies.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
 <a href="asp_applications.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />


<p class="intro">The Session object is used to store information about, or change settings for
a user session. Variables stored in the Session object hold information about one single
user, and are available to all pages in one application. </p>
<hr />

<h2>The Session object </h2>


<p>When you are working with an application, you open it, do some changes and then
you close it. This is much like a Session. The computer knows who you are. It
knows when you start the application and when you end. But on the internet there is one
problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain
state. </p>


<p>ASP solves this problem by creating a unique cookie for each user. The cookie
is sent to the client and it contains information that identifies the user. This
interface is called the Session object. </p>


<p>The Session object is used to store information about, or change settings for
a user session. Variables stored in the Session object hold information about one single
user, and are available to all pages in one application. Common information
stored in session variables are name, id, and preferences. The server creates a
new Session object for each new user, and destroys the Session object when the session expires. </p>
<hr />

<h2>When does a Session Start?</h2>
<p>A session starts when: </p>
<ul>
  <li>A new user requests an ASP file, and the Global.asa
    file includes a Session_OnStart procedure</li>
  <li>A value is stored in a Session variable</li>
  <li>A user requests an ASP file, and the Global.asa file uses the &lt;object&gt; tag to instantiate an object with session
    scope</li>
</ul>
<hr />

<h2>When does a Session End?</h2>
<p>A session ends if a user has not requested or refreshed a page in the application for a specified period. 
By default, this is 20 minutes. </p>

<p>If you want to set a timeout interval that is shorter or longer than the default, you can
set the <b>Timeout</b> property.</p>

<p>The example below sets a timeout interval of 5 minutes:</p>

<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>
        <pre>&lt;%
Session.Timeout=5
%&gt;</pre>
      </td>
    </tr>
</table>

<p>To end a session immediately, you may use the <b>Abandon</b> method:</p>

<table class="ex" cellspacing="0" border="1" width="100%">
<tr><td>
<pre>&lt;%
Session.Abandon
%&gt;</pre>
</td>
</tr>
</table>

<p><b>Note:</b> The main problem with sessions is WHEN they should end. We do 
not know if the user's last request was the final one or not. So we do not know 
how long we should keep the session &quot;alive&quot;. Waiting too long for an idle 
session uses up resources on the server, but if the session is deleted too soon 
the user has to start all over again because the server has deleted all the 
information. Finding the right timeout interval can be difficult!</p>


<p><b>Tip:</b> If you are using session variables, store SMALL amounts of data
in them. </p>


<hr />

<h2>Store and Retrieve Session Variables </h2>


<p>The most important thing about the Session object is that you can store variables in it.</p>


<p>The example below
will set the Session variable <i>username</i> to &quot;Donald Duck&quot; and the Session variable <i>age</i>
to &quot;50&quot;:</p>


<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>
        <pre>&lt;%
Session(&quot;username&quot;)=&quot;Donald Duck&quot;
Session(&quot;age&quot;)=50
%&gt;</pre>
      </td>
    </tr>
</table>


<p>When the value is stored in a session variable it can be reached from ANY page
in the ASP application:</p>


<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>
        <pre>Welcome &lt;%Response.Write(Session(&quot;username&quot;))%&gt;</pre>
      </td>
    </tr>
</table>
<p>The line above returns: &quot;Welcome Donald Duck&quot;.</p>


<p>You can also store user preferences in the Session object, and then access
that preference to choose what page to return to the user.&nbsp; </p>


<p>The example below specifies a text-only version of the page if the user has a
low screen resolution: </p>


<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>


<pre>&lt;%If Session(&quot;screenres&quot;)=&quot;low&quot; Then%&gt;&nbsp;
  This is the text version of the page
&lt;%Else%&gt;&nbsp;
  This is the multimedia version of the page
&lt;%End If%&gt;</pre>


      </td>
    </tr>
</table>
<br />

<hr />
<h2>Remove Session Variables </h2>


<p>The Contents collection contains all session variables.</p>


<p>It is possible to remove a session variable with the Remove method.</p>


<p>The example below removes the session variable &quot;sale&quot; if the value of the 
session variable &quot;age&quot; is lower than 18: </p>


<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>


<pre>&lt;%
If Session.Contents(&quot;age&quot;)&lt;18 then&nbsp;
  Session.Contents.Remove(&quot;sale&quot;)
End If&nbsp;
%&gt;</pre>


      </td>
    </tr>
</table>


<p>To remove all variables in a session, use the RemoveAll method: </p>


<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>


<pre>&lt;%
Session.Contents.RemoveAll()
%&gt;</pre>


      </td>
    </tr>
</table>
<br />

<hr />


<h2>Loop Through the Contents Collection</h2>


<p>The Contents collection contains all session variables. You can loop through 
the Contents collection, to see what's stored in it: </p>


<table class="ex" cellspacing="0" border="1" width="100%">
    <tr>
      <td>


<pre>&lt;%
Session(&quot;username&quot;)=&quot;Donald Duck&quot;
Session(&quot;age&quot;)=50</pre>


<pre>dim i
For Each i in Session.Contents
  Response.Write(i &amp; &quot;&lt;br /&gt;&quot;)
Next
%&gt;</pre>
      </td>
    </tr>
</table>

<p>Result:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr><td>
<pre>username
age</pre>
      </td>
    </tr>
</table>
<p>If you do not know the number of items in the Contents collection,
you can use the Count property:</p>

<table class="ex" cellspacing="0" border="1" width="100%">
<tr><td>
<pre>&lt;%
dim i
dim j
j=Session.Contents.Count
Response.Write(&quot;Session variables: &quot; &amp; j)
For i=1 to j
  Response.Write(Session.Contents(i) &amp; &quot;&lt;br /&gt;&quot;)
Next
%&gt;</pre>
      </td>
    </tr>
</table>

<p>Result:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr><td>
<pre>Session variables: 2
Donald Duck
50</pre>
      </td>
    </tr>
</table>
<br />
<hr />
<h2>Loop Through the StaticObjects Collection</h2>
<p>You can loop through the StaticObjects collection, to see the
values of all objects stored in the Session object:</p>
<table class="ex" cellspacing="0" border="1" width="100%">
  <tr>
    <td>
      <pre>&lt;%
dim i
For Each i in Session.StaticObjects
  Response.Write(i &amp; &quot;&lt;br /&gt;&quot;)
Next
%&gt;</pre>
    </td>
  </tr>
</table>
<br />
<hr />
<a href="asp_cookies.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="asp_applications.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -