Home  >  Article  >  Backend Development  >  Detailed explanation of the usage skills of Session in ASP

Detailed explanation of the usage skills of Session in ASP

Y2J
Y2JOriginal
2017-05-18 11:38:201887browse

Anyone who has written a slightly larger ASP knows that the Session object is really easy to use. It can be used to record the user's private data variables, which is both safe and convenient. But do you really know how Session works?

Anyone who has written a slightly larger ASP knows that the Session object is really easy to use. It can be used to record the user's private data variables, which is both safe and convenient. But do you really know how Session works? Perhaps after understanding it, you will no longer dare to use this beloved and hated object. Although the alternative method is a little troublesome, after long-term consideration, it has to be done.

First of all, let’s talk about the benefits of Session. It can be used to record the client’s private data variables. , and will not disappear within the time frame. This is really an important function, especially for systems with members that must be used. Such as the member's login account, time, status, and many real-time data that should be recorded (such as the shopping system recording the items in the user's shopping basket). This information belongs to the private needs of each user. Usually developers use Session record processing.

However, the Session in ASP is composed of Cookies, and the server transmits all the data recorded in the Session to the user's browser in the form of Cookies. Usually general browsers will save these cookies. Whenever the user clicks a link and connects to the server again, the browser will send these cookies back to the server for processing. This is the operating principle of Session. When the amount of data is larger, since it must be sent out and taken back, it not only eats up line bandwidth, but also reduces performance because the server must spend more resources on online processing and reconfiguring memory. Initial action. Now you may be thinking, "I have to use this function, so I have to sacrifice it." However, this article talks about Session. On the one hand, it teaches you to use it less; on the other hand, of course there are alternatives. The next thing that comes up is that it also belongs to Global.asa. Application object.

Application is also good at recording and processing temporary data. Its capabilities and usage are the same as Session, but in comparison, the data it records is public, that is, any user can Shared variable space. Unlike Session, Application does not transfer data to the user and wait for the next connection to read it back. It is directly recorded in the memory on the Server. In comparison, the performance is much faster than Session.

Since the Application object is public, the first thing that must be done is to plan a common area for each user, so that each user can have his own area to record data, in order to achieve the purpose of simulating the Session. There are two methods now:

First, initialize, create and allocate user memory space in advance when the Server is activated. Usually, although this method takes up a lot of resources as soon as the Server is started, it also saves the time every time the Server is started. Users have to go through the trouble of assigning once they are online. But there is a limitation. This method must limit the maximum number of people. Since it is initialized as soon as it is activated, we can only estimate the creation of a certain amount of memory space, so this method is usually used in small programs such as chat rooms.

2. This method should be considered more appropriate for large-scale applications. It adopts a dynamic allocation method and starts allocating resources to the user when the user first connects to the Server. The purpose of these two methods of simulating Session is to reduce the consumption of Session resources, but after all, they cannot be completely replaced. We still need to use a little bit of Session, which at least can reduce a lot of burden on the Server.

The first solution

First we start the implementation of the first solution. Since the Application is initialized during activation, of course we have to start from Global.asa:

Already The initialization is completed, but how to use it? We only need to change the data originally stored using Session, such as account number and login time, into the Application object we created where the user logs in:

The code is as follows:

'
寻找未被使用的空间 
For
 i = 1 To Application("ClientMax") 
If
 Application("User_Status_" & i) = 0 Then 
'使用者暂时编号 
Session("Index") = i 
'锁定 
Application Application.Lock 
'设成已使用的状态 Application("User_Status_" & i) = 1 '放入变量数据 
Application("User_Ac
count
_" & i) = Account 
Application("User_Log
time
_" & i) = Now()
'解除锁定 
Application.Unlock 
Exit For 
End
 If 
Next


To obtain user-related variable data, do as follows:

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

You may find that you don’t want to use Session? Then why does Session exist in the above source code? As mentioned before, this alternative cannot completely replace Session. The browser is not always online with the Server. status, the connection will be disconnected after reading the page, so how do we know that the same person will be connected next time? At this time, we must rely on Session. We give the user a set of real-time numbers, and this number is the user's application. For the number in the variable space, you can imagine that there are many safes in the bank. You have a key, and there is a number on the key. The number on the key allows the clerk to lead you to your own safe. This method still exists. There are improvements, but it is enough for small applications

The second solution

Regarding the previous solution, you may also think that our customized number uses Session. Let's record, when it comes to numbering, the Session object provides a "SessionID" method. That's right, whether we want to use it or not, the Server will automatically assign a number to each user, and this number will not be repeated. As for this number, Session. SessionID is obtained. This numbering is an action that Session will definitely do. We can use it to replace the numbering program we wrote ourselves, which also saves a lot of effort and even has greater scalability. But basically, the first one above. This solution still has its uses, such as small applications such as chat rooms that limit the number of people. The next second alternative is for larger systems.

Number of users per second. For websites with hundreds, thousands or even tens of thousands of people, using the previous solution will definitely not work. Suppose you set the upper limit of 10,000 people. Once activated, the server will help you cut out 10,000 areas for use by 10,000 people. Or, if there are 5 variables in an area, and one variable occupies 32 bytes, 10,000 will occupy more than 320,000 K (320MB). As soon as the Server is activated, so much garbage will be stuffed into the memory, and the performance will inevitably be insufficient. It will be reduced a lot when going to the battlefield; and despite the small number of these numbers, I thought that my 512 MB would be enough. The above numbers are assuming a minimum number, plus it is unknown how many additional resources the server will use when configuring memory. So it will only be more, not lower. Therefore, the only solution is to Dynamic ConfigurationUser variable space, only cut out an area when a user is connected to the server, so there is no need to configure a huge amount in advance. Memory.

The second option is relatively simple to implement. Please throw away all the things in the first option. We don’t need to move to Global.asa. We only need to change the user login place and other useful places:

The code is as follows:

'锁定 ApplicationApplication.Lock '放入变量数据 
Application("User_Account_" & Session.SessionID) = Account 
Application("User_Logtime_" & Session.SessionID) = Now() '解除锁定Application.Unlock

To obtain the user’s relevant variable data, do as follows:

The code is as follows:

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

[Related recommendations]

1. ASP free video tutorial

2. ASP session simple example

3. Detailed introduction to session in ASP

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

5. Introduction to Session in ASP Three methods of objects

The above is the detailed content of Detailed explanation of the usage skills of Session in ASP. 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