ASP Session Simple Example

Source: Internet
Author: User
Session Object
You can use the session object to store the information that is required for a specific user session. This way, when a user jumps between the application's Web pages, the variables stored in the session object are not lost, but persist throughout the user's session.
When a user requests a Web page from an application, if the user does not yet have a session, the Web server automatically creates a Session object. When the session expires or is discarded, the server terminates the session.
One of the most common uses of Session objects is to store the user's preferences. For example, if a user indicates that they do not like to view a graphic, they can store the information in the Session object. For more information about using session objects, see "Managing Sessions" in the ASP Application section.
Note Session state is reserved only in browsers that support cookies.
Grammar

Session.collection|property|method

Collection
Contents contains the items that were added to the session with a script command.
The StaticObjects contains objects created with the <OBJECT> tag and given the session scope.

Property
CodePage The code page that will be used for the symbol map.
LCID field identification.
SessionID returns the user's session validation.
Timeout time-out for application session state, in minutes.

Method
Abandon This method destroys the Session object and frees its resources.
Event
A script that declares the following events in the Global.asa file.
Session_OnEnd
Session_OnStart

For more information about the above events and global.asa files, see Global.asa Reference.
Comments
You can store values in the Session object. The information stored in the session object is valid within the sessions and session scopes. The following script shows how the two types of variables are stored.

<% session ("username") = "Janine" session ("age") =%>

However, if you store objects in the Session object, and you use VBScript as the primary scripting language. You must use the keyword Set. As shown in the following script.

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

You can then invoke the methods and properties revealed by Mycomponent.class1 on a subsequent Web page by invoking the following methods:

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

You can also call by expanding the local copy of the object and using the following script:

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

Another way to create a session-scoped object is to use the <OBJECT> tag in the Global.asa file.
However, you cannot store the built-in object in the Session object. For example, each of the following lines will return an error.

<% Set session ("VAR1") = Session Set session ("var2") = Request Set session ("VAR3") = Response Set session ("VAR4") = Ser Ver Set Session ("var5") = Application%>

Before you store an object in a Session object, you must understand which threading model it uses. Only objects marked as "Both" can be stored in a session object that does not have a single-threaded session locked. For more information, see "Selecting a threading Model" in "Creating ASP components."
If you store an array in the Session object, do not directly change the elements stored in the array. For example, the following script cannot be run.
<% Session ("StoredArray") (3) = "New Value"%>
This is because the session object is implemented as a collection. The array element StoredArray (3) did not get a new assignment. This value will be included in the Application object collection and will overwrite any information previously stored in this location.
We strongly recommend that you get a copy of the array before retrieving or altering the objects in the array when storing the array in the Session object. In an array operation, you should then store all the arrays in the Session object so that any changes you make will be stored. This is demonstrated by the following script.
---file1.asp---

<% ' Creating and initializing the array Dim MyArray () Redim MyArray (5) MyArray (0) = "Hello" MyArray (1) = "Some other St Ring "' Storing the array in the Session object session (" StoredArray ") = Myarrayresponse.redirect (" file2.asp ")%>--- File2.asp---<% ' retrieving the array from the session Object ' and modifying its second element LocalArray = Session ("S Toredarray ") LocalArray (1) =" There "' printing out the string" Hello there "Response.Write (LocalArray (0) &localarray ( 1) ' re-storing the array in the Session object ' this overwrites the values in StoredArray with the new values Session ("Sto Redarray ") = LocalArray%>

Related answers:
A page needs to enter the correct user name and password,
Add clause: Session ("IsLogin") =true
In front of page B to judge:

If session ("IsLogin") =false then Response.Write "Not signed in" Response.en End If

That's it.
Post Login settings:
Session ("user") = user name
on page B, enter:

If session ("user") = "Then Response.Write (" <script>alert (' Please login! '); Window.history.go ( -1);</scritp> ") End If

"Recommended"

1. asp free Video Tutorial

2. A detailed introduction to the session in ASP

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

4. Three ways to introduce session objects in ASP

5. Detailed use of Session in ASP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.