Home Backend Development C#.Net Tutorial Detailed explanation of the usage skills of Session in ASP

Detailed explanation of the usage skills of Session in ASP

May 18, 2017 am 11:38 AM

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
Copy after login


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
Copy after login

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

The code is as follows:

Response.Write(Application("User_Account_" & Session.SessionID))
Copy after login

[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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set session timeout in SpringBoot Session How to set session timeout in SpringBoot Session May 15, 2023 pm 02:37 PM

The problem was found in the springboot project production session-out timeout. The problem is described below: In the test environment, the session-out was configured by changing the application.yaml. After setting different times to verify that the session-out configuration took effect, the expiration time was directly set to 8 hours for release. Arrived in production environment. However, I received feedback from customers at noon that the project expiration time was set to be short. If no operation is performed for half an hour, the session will expire and require repeated logins. Solve the problem of handling the development environment: the springboot project has built-in Tomcat, so the session-out configured in application.yaml in the project is effective. Production environment: Production environment release is

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

What should I do if the php session disappears after refreshing? What should I do if the php session disappears after refreshing? Jan 18, 2023 pm 01:39 PM

Solution to the problem that the php session disappears after refreshing: 1. Open the session through "session_start();"; 2. Write all public configurations in a php file; 3. The variable name cannot be the same as the array subscript; 4. In Just check the storage path of the session data in phpinfo and check whether the sessio in the file directory is saved successfully.

What is the default expiration time of session php? What is the default expiration time of session php? Nov 01, 2022 am 09:14 AM

The default expiration time of session PHP is 1440 seconds, which is 24 minutes, which means that if the client does not refresh for more than 24 minutes, the current session will expire; if the user closes the browser, the session will end and the Session will no longer exist.

How to solve the problem that the Springboot2 session timeout setting is invalid How to solve the problem that the Springboot2 session timeout setting is invalid May 22, 2023 pm 01:49 PM

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,

How to implement SMS login in Redis shared session application How to implement SMS login in Redis shared session application Jun 03, 2023 pm 03:11 PM

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if

What are the differences between JavaScript and PHP cookies? What are the differences between JavaScript and PHP cookies? Sep 02, 2023 pm 12:29 PM

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site

See all articles