Programming | Advanced 3rd Chapter ASP Application session
In the previous sections, the ASP's approach to accessing a customer request and generating responses is described in this chapter, which discusses the other two objects of ASP. is the application and the Session object. These two objects are not directly related to the management of requests and responses, but more to the management of the environment in which the ASP Web pages are run.
One of the common problems associated with building a Web site or Web application is that there is no state when using the HTTP protocol. The state provides variable values, objects, and other resources related to a specified user, and can be used by any routines in the application, and the state can do some work when writing a generic, customer-based application in a programming language such as VB or C + +. However, the web does not provide this capability. In this chapter, readers will see why and how to avoid this problem.
Some terminology and technical issues are also covered in this chapter. It has so far simply discussed "Web applications" in this book, but does not really understand or exactly define exactly what they are. This book also refers to the "user session", there is no corresponding relatively complete description. This is intentionally omitted because they are closely related to the ASP's application and session. The ASP's application and session objects are described below.
The main contents of this study:
· What Web applications are, and how they relate to ASP application objects.
· How the ASP automatically creates and manages applications and sessions.
· Functions provided by the application and session objects.
· How to put application and Seesion objects into an ASP Web page.
First of all, the core problem of the whole content: state.
3.1 State management on the Web
Many developers have never considered the concept of State before transferring an application to the Web. As mentioned earlier, the web is a stateless environment. So you should explore what states are and how you can avoid problems.
Exact definition of 3.1.1 State
In a single-user program, when you create an executable application, such as using VB to create an. exe file, you can declare a global (or public) variable and access it anywhere in your code. At all times the application runs, the time value is always valid and accessible.
For a traditional client/server solution, such as a system in which a client-based application accesses a server-based database engine, each client establishes a connection to the server and the database application. This connection is usually established by validating the user's methods.
The validation process is a typical process of identifying a user, using a combination of username and password to prove whether it is a legitimate user.
Once authenticated, a connection is established between the client and the server-based application, which remains in effect for all the time the user uses the application. This happens when the user registers on the yeast Windows 2000 Server. Whenever an administrator uses the Active Directory Users and Computers utility (click Directory Management in the administrative Tools option on the Start menu) Item), you can observe the active user connection. This process is the same in many systems, such as Microsoft SQL Server.
This permanent connection means that when a user sends instructions or requests to a server, the server can easily identify each user. The same server's response or any other user's information can also be returned directly to the user. It is further noted that the server can easily store the values and information associated with each customer and provide it to the appropriate customer when needed. Of course, a server application can have a primary global variable to make it accessible to users when they need it.
This ability to recognize each client's request and hold the value of the associated user in memory constitutes a state. You can think of the state as representing the value of the application, the environment, and the user's internal variables, and throughout the process of connecting the application and the user.
The importance of 3.1.2 State
If you plan to create web-based applications that interact with users, rather than just Web sites that display stand-alone pages, you must be able to provide a separate state for each user. This may be just remembering their names, or you might want to store an object reference or a different recordset for each user. If you can't do this, the ASP Web page can't do much more, because when the page executes, the variables and other related information in the page are corrupted. When the hired user requests the next page, all the information provided by this page will be lost.
Therefore, you need to find a way to save the status of each visitor. It is important to be able to store global values for all users. For example, a web-style access or page hit counter, which does not provide its own counters for each user, often wants to see the total number of visitors, not just the number of times they have visited themselves. The number of visitors needs to be stored with the application-level state, not with the user-level state.
This is not an emerging problem, since commercial sites occupy the web, already exist, even earlier. So there are a lot of traditional solutions for storing state on the web. Web site administrators want to know if visitors have visited their sites before, how many times have they visited them? Also regularly visit other sites and so on. This will better set its advertising goals. All of these require a way to store information about the Web page requests or between each visit that the user generated during the visit.
3.1.3 Create a state on the web
A common way to provide state between page requests and site access is through cookies. As we have seen in the previous chapters, how to store values in the client's computer that are sent to the domain that is valid for this cookie with each page request. By checking and updating cookies with ASP, you can maintain a state in some way. You can use the information that is included to identify the user, and then connect the user to a collection of stored corresponding values.
For example, you can detect whether a user request contains a site-specified cookie. If not, assign the user a certain type of identity, indicating a quantity and stored in a cookie with a long validity period. After each visit to the site, the user can detect the cookie and update the contained information. You can also collect data about the number and duration of visits and store them on the server for future use.
But what happens if a user moves to another computer, deletes a cookie, or their browser refuses to receive a cookie sent to them? In this case, the state cannot be maintained because the next time they do not recognize them, there are many cookies on the web, and most people will accept them without notice. If you open the "Warn before accepting cookies" option in your browser, and then roam through several large sites, you'll see what that means.
1. Anonymous and authorized audiences
If you think cookies are a somewhat sloppy solution, you can use a more straightforward approach. One way many sites adopt is when a visitor clicks on a site, or clicks on a page that requires authentication, a dialog box pops up. Visitors must first register to obtain a combination of some type of username/password to allow access to the appropriate site or page.
To verify that the visitor is a known and legitimate user, a cookie placed on the visitor's computer, it either saves the registered details, or a "key" that indicates that the identity has been authenticated. At the same time, the visitor's detailed data is permanently stored on the server and ready to be accessed again. If a visitor has such a cookie in their browser, he is free to visit the site because it has been validated.
If the cookie has no validity period (Expires), the value of the cookie disappears automatically when the browser is closed, and must be re-register and validated again on the next visit. Of course, if you refuse to receive a cookie or delete a cookie, you can only get the Registration dialog box again. In this way, if you are not recognized, you cannot access the site.
By forcing users to register to a Web server as if they were registered with their own network, the overall security of Windows 2000 provides a stronger and more secure authentication capability for IIS. However, this can only work with Internet Explorer 3.0 and version of the browser. IIS can also use Basic authentication to allow non-Microsoft browsers to register a Web server.
2. No more anonymous visitors
When using ASP on an IIS Web server, users can be tracked in the current session unless the user leaves the site to another Web site or closes the browser. Later in this chapter, you will see how to use this feature to identify a visitor, store the local information for a user, and provide status. The following is a discussion of how it works, compared to the solutions that have been discussed.
ASP and IIS together propose the concept of a user session, which interacts with the ASP Sessions object. When each visitor first accesses an ASP page on the server, creates a new and separate session object for him, assigns a session identification number to the session, and sends a cookie of the specially encrypted version containing the session identifier to the customer.
The path to the cookie (refer to the previous section for a description of the cookie property) is set to the root path of the ASP application running on the server. This is likely to be the root of the default Web site (that is, "/"), but it may also be a different value (see later). The expires value is not provided in the cookie, so the cookie value disappears when the browser is closed.
Whenever this user accesses the ASP page, the ASP will look for the cookie. Named Aspsessionidxxxxxxxx, where each x is an alphabetic character. From the ServerVariables collection shown in chapter 2nd Figure 2-7, you can see it in the HTTP header. The ASP cookie is highlighted here, as shown in Figure 3-1: