Home >Backend Development >C#.Net Tutorial >ASP session simple example

ASP session simple example

Y2J
Y2JOriginal
2017-05-18 11:40:381725browse

Session Object
You can use the Session object to store information needed for a specific user session. In this way, when the user jumps between the application's Web pages, the variables stored in the Session object will not be lost, but will persist throughout the user session.
When a user requests a Web page from an application, the Web server will automatically create a Session object if the user does not already have a session. When a session expires or is abandoned, the server terminates the session.
One of the most common uses of the Session object is to store user preferences. For example, if the user indicates that he or she does not like to view graphics, this information can be stored in the Session object. For more information about using the Session object, see "Managing Sessions" in the "ASP Application" section.
Note Session State is only retained in browsers that support cookie.
Syntax

Session.collection|property|method

Collection
Contents Contains items that have been added to the session using script commands.
StaticObjects Contains objects created with the 7a4d48e0cef2cd71d96cba7e2b26153a tag and given session scope.

Properties
CodePage The code page that will be used for symbol mapping.
LCID Site Identification.
SessionID Returns the user's session verification.
Timeout The timeout period for the application session state, in minutes.

Method
Abandon This method destroys the Session object and releases its resources.
Events
The script for the following events is declared in the global.asa file.
Session_OnEnd
Session_OnStart

For more information about the above events and the global.asa file, please refer to the Global.asa reference.
Notes
You can use the Session The value is stored in the object. Information stored in a Session object is valid within the session and session scope. The following script demonstrates how two types of variables are stored.

<% 
Session("username") = "Janine" 
Session("age") = 24 
%>

However, if you store the object in the Session object, and you use VBScript as the main scripting language. The keyword Set must be used. As shown in the following script.

<% Set Session("Obj1") = Server.CreateObject("MyComponent.class1") %>

Then, you can call the methods and properties revealed by MyComponent.class1 on the subsequent Web page. The calling method is as follows:

<% Session("Obj1").MyMethod %>

You can also expand it by Make a local copy of the object and call it using the following script:

<% 
Set MyLocalObj1 = Session("Obj1") 
MyLocalObj1.MyObjMethod 
%>

Another way to create a session-scoped object is to use the 7a4d48e0cef2cd71d96cba7e2b26153a tag in the global.asa file.
But built-in objects cannot be stored in Session objects. For example, each line below will return an error.

<% 
Set Session("var1") = Session 
Set Session("var2") = Request 
Set Session("var3") = Response 
Set Session("var4") = Server 
Set Session("var5") = Application 
%>

Before storing an object into a Session object, you must understand which thread model it is using. Only those objects marked "Both" can be stored in a Session object that does not lock a single-threaded session. For more information, see "Selecting a threading model" in "Creating ASP Components".
If you store an array in a Session object, please do not directly change the elements stored in the array. For example, the following script cannot be run.
1751e3f72d4c82754921cb8852945515
This is because the Session object is implemented as a collection. The array element StoredArray(3) did not get a new value. This value will be included in the Application object collection and will overwrite any information previously stored at this location.
We strongly recommend that when storing an array in a Session object, you obtain a copy of the array before retrieving or changing the objects in the array. When operating on an array, you should store the entire array in the Session object so that any changes you make will be stored. The following script demonstrates this.
---file1.asp---

<% 
&#39;Creating and initializing the array 
Dim MyArray() 
Redim MyArray(5) 
MyArray(0) = "hello" 
MyArray(1) = "some other 
string
"
&#39;Storing the array in the Session object 
Session("StoredArray") = MyArray
Response.Re
dir
ect("file2.asp") 
%>
---file2.asp--- 
<% 
&#39;Retrieving the array from the Session Object 
&#39;and mod
if
ying its second element 
LocalArray = Session("StoredArray") 
LocalArray(1) = " there"
&#39;
print
ing out the string "hello there" 
Response.Write(LocalArray(0)&LocalArray(1))
&#39;Re-storing the array in the Session object 
&#39;This overwrites the values in StoredArray with the new values 
Session("StoredArray") = LocalArray 
%>

Related answers:
After entering the correct user name and password on page A,
Add a statement: session("isLogin ")=true
Judge before page B:

if session("isLogin")=false then 
response.write "未登录" 
response.en 
end if

That’s it
Set after login:
session("user")=username
In B page input:

if session("user")="" then 
Response.write("<script>alert(&#39;请登陆!&#39;);window.history.go(-1);</scritp>") 
end if

[Related recommendations]

1. ASP free video tutorial

2. About ASP Detailed introduction to session

3. Teach you how to solve the problem of ASP session loss

4. Introduce three methods of Session object in ASP

5. Detailed explanation of the skills of using Session in ASP

The above is the detailed content of ASP session simple example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn