An in-depth study on the scope of Web applications and the loss of session variables

Source: Internet
Author: User
session|web| Variable | procedure | problem

Foreword: Recently many netizens have this kind of question, is when we set a session variable between a webpage, to another webpage, but disappeared. This
What is the reason. If you finish reading this article in detail, I believe I can give you a complete reply!

One: Preface--About Web applications

The Web application we are discussing today refers to a Web application that is provided by the Windows NT Option Pack to establish an Internet or intranet
Order of the platform. Web applications can be delivered as a set of Web pages that encapsulate transaction logic and provide access to databases that store important business information
The application and ActiveX components provide a user interface. Which is the ASP program we use.
When you create an application, you must specify the application's startup point directory in the Web site with Internet Services Manager. Before other startup points are found,
Each file and directory in a Web site's startup point directory is considered part of the application. Therefore, you can use directory boundaries to define the scope of your application.
An ASP-based application is a collection of ASP pages and ActiveX components. When a user defines an application, it uses IIS to specify the user's Web site
The directory of the application startup point. Each file and folder that is located in the startup point directory in the user's Web site is considered part of the application until
Find an additional boot point directory. This allows users to define the scope of the application using the directory as a boundary. Each Web site can have multiple applications
Programs, and each application can have a different configuration.

Second: The scope of the Web application

As mentioned in the definition above. We will be a Web application, such as: shopping programs, news programs, chat programs ... And so some of us develop
ASP pages are individually concentrated to become a Web application. In combination with examples, such as we will a shopping program for all pages (such as: registration page, Shopping
Car, cashier, order page, merchandise page) unified, collectively referred to as a web shopping program, its application scope is from shopping to start shopping end
And to all of the pages. In IIS, the system must assign it to a virtual directory, and all pages of that virtual directory, and their subdirectories, are their shopping threads
The physical range of the order. As shown in the following table.
*shop-index.asp
-buy.asp
+catalog-list.asp
+customer-a.asp
-b.asp
The * number, which represents a virtual directory, + represents the subdirectory. So, all of the above pages make up a Web application. But
You can also build another application under one virtual directory. As shown in the following table
*shop-index.asp
-buy.asp
*book-book.asp
+catalog-list.asp
+customer-a.asp
-b.asp
In this way, the shop under the index.asp,list.asp,a.asp belong to an application, and the book under the book.asp is not part of the shop, he is a new web should
With the program.

Three: Web application scope settings and applications

If your development environment is visual InterDev6.0, then the application settings are rarely bothered to consider, and everything has VI6 to set you up.
, and automatically creates a virtual directory in IIS, but if it is something else, you might want to set it yourself in IIS. The specific steps are as follows:
Creating an Application
1--in Internet Services Manager, select the directory that is the application startup point. You can specify the home directory of the Web site as the application
The starting point of the order.
2--Open the property page for the directory, and then click the home directory, virtual directory, or Directory tab.
3--in the Name text box, type a name for the application. ' Of course, the name of the application is also not necessary. But recommended settings.
4--Click the Create button.
After that, you can map your Web application directory to that directory.
Understanding the basic definitions above is necessary for our network developers, because it is because of the scope of a Web application that Web applications can
Programs can share information between applications ' files, for example, an ASP application can share environment flows, session state, and variable settings among its Web pages. which is me.
Session,application objects that are often used. should be defined as only an application and its scope, session,application and other objects are intended
Yi.

Four: The session variable "lost" problem

Talking about so many concepts, finally can say the substantive question, the session object is one of the most important image of the Web application, it is the existence
Allows us to transfer data across web pages to distinguish between different users. We just have to make the variables to the session to achieve. This process is
Session ("temp") can be =temp. But recently many netizens have this kind of question, is when we set up a session variable between a webpage, to another
A Web page, but disappeared. What is the reason for that?
Based on the above concept, we can summarize the reasons, and the processing methods are summarized as follows.
1:asp Web pages span multiple Web applications:
The reason is that one of your other Web pages might be another. In the past, some data excessively emphasized the singleness of conversational variables, like a
After a user connects to a web host, it is not correct to create only one session variable, which is based on the web that we mentioned above
Using the scope of the program, rather than the individual based on the user, you can try the following section of code.
Test.asp
<% @ language=vbscript%>
<%
Session ("temp") = "temp"
Response.Write session. SessionID ' SessionID is used to identify individual customers between applications.
%>

Copy the test.asp to two different virtual directories, respectively. You can see that he's two different values. So maybe your pages across a different should
With the program.
Of course, this situation for beginners may encounter, for experienced people may make the mistake that they themselves are a root directory, but in the development of virtual
The subdirectory is set to a virtual directory, just like the second table structure we mentioned above. So he's also become two different web
Application. It's not hard to explain why it went wrong.

2: It is possible to start multiple identical applications.
The first situation is very good to find, also easy to correct, but this second kind of situation is not easy to find, also more difficult to correct. Here we introduce a concept of another. Many
The same application coexistence, (very long, the key is the concept of Microsoft I forgot, this is my own.) Oh! ), it means well from this definition
Understanding, is for the same application. Multiple can exist in memory at the same time. As with our common Acdsee.exe, you can click to launch multiple
Acdsee.exe. And the other is not, we do not say its definition (if said, is the same application does not coexist, hehe!) ) at the same time
, if an application has been started, the same application application cannot be started. Like Foxmail.exe, when you start a
After Foxmail.exe, only one foxmail can exist in memory. If you are starting, you will not open a foxmail window again.
And our IE belongs to the former, you can start multiple IE applications by clicking ie. At this point, we do another test below. You will be the code above
Test.asp into a virtual directory. Then, you click IE Start (note: It is started by clicking IE, not by CTRL + N, nor in an IE window dot
Click the file, reopen a window) at this point, you can find that although they are the same address, the same user, but SessionID is still different, of course,
You can also build another file. Such as
Test2.asp
<% @ language=vbscript%>
<%
Response.Write Session ("temp")
%>
At this point, you are under another IE window, you will find that the session ("temp") is empty, that is, we just clearly attached to the test.asp value, and
Test2.asp and test.asp are in the same virtual directory, but session ("temp") is lost. Originally, relative to the same ASP application. The different IE relative
are different ASP applications. (a bit like a tongue twister, you know.) However, we have understood the cause of it and believe it is not difficult to solve him.
Also note here that we have just repeatedly mentioned that by clicking IE, not by pressing CTRL + N, nor by clicking on a file in an IE window, re
To open a window, this is to say that if you open a window in an IE window, it belongs to a derived window, not a symbiotic window, that is, at this point
Another window is inherited from the previous window and belongs to the same application scope.
3: Improper connection
In fact, most of the users encountered the problem is by clicking on the page to connect to the new open window and found that the session variable lost. He presses CTRL + N to open a new window
The same meaning. But why is there a problem now? Let's analyze how it connects. May be <a href= "xx.asp" Traget=_blank>click
Me </a> the connection by such a click is the same as CTRL + N and should not go wrong. There is also a connection through script-controlled
window.open "xx.asp", "name", "Toolbar=no" code like this, he could have such a problem. If you open a multiple Symbiosis window by clicking IE
(many netizens want to save time, they like to open multiple windows.) Like me. )。 Is that he might be in the newly opened window if the window contains the same name.
At this point, may be improper connection caused session loss, in fact, the reason is the same as the second. There is also a more extreme way to open a new window by using
Document.Write in a script to write a new page dynamically, and the URL of the page does not exist, which is more likely to be problematic, should be the session is
Tracked through the url,http. So if the page address is dynamic. Then the session is not traceable at all.

It is believed that the analysis of the above examples may explain most of the problems that arise in your application session. But not all (if you disable cookies,
Oh! However, if you understand all of the above concepts, you will be able to solve the problems that arise in your application session.



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.