A detailed explanation of the use of Session in ASP

Source: Internet
Author: User
People who have written a little bit of a large ASP know that the Session is a very useful object, it can be used to record the user's private data variables, both safe and convenient. But do you really know how the Session works?

People who have written a little bit of a large ASP know that the Session is a very useful object, it can be used to record the user's private data variables, both safe and convenient. But do you really know how the Session works? Perhaps you will never dare to use this beloved and hated object again after knowing it. While the alternative approach is a bit cumbersome, it has to be done in the long term.

First of all, the advantages of the Session, it can be used to record the client private data variables, and in the time frame will not disappear. This is really a very important feature, especially if there is a member system that needs to be used. such as a member's login account number, time, status and a lot of real-time data on the record ﹝ such as the shopping system records the user's shopping basket of goods ﹞, this information belongs to each user's personal needs, usually developers are using Session record processing.

However, the session in the ASP is made up of cookies, and the server passes all the data recorded in the session to the user's browser in the form of cookies. Usually the browser will save these cookies, the browser will send these cookies back to the server for processing whenever the user contacts the link and once again comes online with the server. This is the operation of the Session principle, when the data volume is larger, because must be sent out and received back, not only to eat line bandwidth, performance is relatively low, because the Server must spend more resources in the online processing and reconfiguration of memory and other initial actions. Now you may think, "I have to use this function, I have to sacrifice a bit", but this article on the one hand is to teach people to use less, on the other hand of course there is an alternative, followed by the same genus Global.asa within the Application object.

Application is also a skilled record of temporary data processing, all aspects of the ability and usage of the same as the Session, but compared to the information it records are public, that is, any user can share the variable space. Application not like Session, not to pass the data to the user, and so on the next time to read back online, it is directly recorded in the Server memory, relatively high performance on the session many.

Because the Application object is public, the first thing to do is to plan a common area for each user, so that each users have their own region can record data, in order to achieve the purpose of the simulation Session. Now there are two ways to do this:

First, in the server activation of the pre-initialization to establish and allocate user memory space, usually this practice although a server power-up first accounted for a lot of resources, but also save the future whenever the user online must do a distribution of trouble. But there is a limitation that using this method must limit the maximum number of people, because it is initialized by an activation, we can only estimate to establish a certain amount of memory space, so this method is usually used in chat room this small program.

Second, this method should be more appropriate for large applications, using dynamic allocation method, when the user first online to the Server before the allocation of resources to the users. These two simulation sessions, the purpose of the session is to reduce the consumption of resources, but after all, can not be completely replaced, we still need to use a little session, at least the Server has been able to alleviate a lot of burden.

First Scenario

First we start with the implementation of the first scenario, since it is the initialization of the application at activation, of course we have to proceed from the Global.asa:

The initialization is complete, but how do I use it? We only need to place the user login, the original use of the Session stored data, such as account number, login time, changed to our established Application object can be:

The code is as follows:

' Find unused space for i = 1 to Application ("Clientmax") If application ("User_status_" & i) = 0 Then ' user temporarily numbered Session ("Index") = I ' Lock application Application.Lock ' is set to the Used state application ("User_status_" & i) = 1 ' Put variable data application ("User_account_ "& i) = Account Application (" User_logtime_ "& i) = Now () ' Unlocked application.unlock Exit for End If Next


To get the relevant variable data for the consumer, it's like this:

Response.Write (Application ("User_account_" & Session ("Index"))

You may find, not to say not to use the Session? Then why is there a Session in the original code above? As mentioned earlier, this alternative does not completely replace the Session, the browser is not always and the Server is online, read the page is disconnected, then how do we know the next time online or the same person? At this point, we have to rely on the Session, we give the user a set of real-time numbering, this number is the user on the application variable space number, you can imagine the bank has a lot of safe, you have a key, and the key is numbered, The number on the key allows clerk to lead you to your own safe. There are improvements to this approach, but it is sufficient for small applications.

Second programme

As for the previous scenario, you might also think that our custom numbers are recorded using the session, and that the session object has a "SessionID" method to provide. Yes, regardless of whether we want to use it, the Server will automatically make a number for each user, and this number will not repeat, as far as this number is Session.SessionID. This number is the Session will certainly do the action, we can use it instead of our own written numbering procedures, but also save a kung fu, even greater expansion. But basically, the first scenario above still has its use in, like the limit number of chat rooms and so on small applications, the next second alternative is for the larger system.

The number of stations per second up to hundreds of thousands of or even tens of thousands of people on the site, the use of the previous program, must not be feasible. Assuming you set the maximum number of 10000, server activation will help you cut out 10,000 regions to prepare for 10,000 users, if there are 5 variables in a region, a variable of 32 bytes (byte), 10,000 of the 320000 K (320MB), the server one activates Stuffed so much rubbish into memory, performance is bound not to go to the battlefield to reduce a lot; and don't look at these numbers, think their MB will suffice, the above figure is assumed a minimum number, plus the Server in the configuration of memory when the additional use of resources is unknown, so only more will not be lower. So the solution is only to dynamically configure the user variable space, when there is a user and the Server online to cut a piece of the area out, so there is no need to configure a large amount of memory beforehand.

The second scheme is relatively simple to do, please throw away all the items of the first plan, we do not need to move to the Global.asa, only need to change the user login and other useful places:

The code is as follows:

' Lock Applicationapplication.lock ' into variable data application ("User_account_" & Session.SessionID) = Account Application (" User_logtime_ "& Session.SessionID) = Now () ' Unlocked application.unlock

To get the relevant variable data for the consumer, it's like this:

The code is as follows:

Response.Write (Application ("User_account_" & Session.SessionID))

"Recommended"

1. asp free Video Tutorial

2. ASP Session Simple Example

3. A detailed introduction to the session in ASP

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

5. Three ways to introduce session objects 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.