Cookie and Session to implement pseudo-Session

Source: Internet
Author: User

I have read some articles about cookies and sessions on the Internet, and I will summarize them myself.

1. Cookie: divided into two categories

A) session cookies are stored in the browser memory. The expiration time is not specified and will expire when the browser is closed.

B) the Cookie stored on the client's hard disk, which specifies the expiration time, will be saved in the client's file

2. Session: user data stored on the server can depend on cookies or cookies,

When Cookie is dependent, the client and server determine the identity of the client user by passing a SessionID to each other.

When the Cookie is not dependent, you can use the Url parameter to determine the identity of the client user. The format is

Http: // localhost/UrlReWrite/(S (gzlhzp55q51okj45nx20scvk)/Main. aspx the red part is the encrypted SessionID.

The process is as follows:

 

 

The Session is saved on the server. By default, sessions that are not active are cleared in 20 minutes. Clearing is a server action, not a client action. Therefore, even if the browser is closed, the Session has not expired and remains on the server. If you use a URL to transmit the SessionID, when you continue to use the previous SessionID to access the server, the corresponding user information is also found.

 

 

On the CSDN forum, I saw a question about how to simulate a pseudo Session (one interview question) if the Session on the server is disabled. If it is me, I will solve it like this:

 

1. The server creates a database with two tables as follows:

Table 1

MySessionID Nvarchar (50) Unique

CheckInDate DateTime

 

Table 2

MySessionID Nvarchar (50)

Key Nvarchar (50)

Value Nvarchar (50)

 

The role of table 1 is to maintain the mySessionID for user identity confirmation (this name is used to distinguish it from the system's SessionID)

Table 2 is used to store user-related data.

 

1. When the client accesses the server for the first time, I will generate a mySessionID for this Request through computation (which is beyond the scope of consideration here) and add it to table 1, the data stored on the server will be stored in table 2.

2. When Response is sent to the client, add the mySessionID to the Cookie and return it to the client. Then, the next Request from the client can pass its mySessionID to the server through Cookie. After obtaining this mySessionID, the server updates the corresponding CheckInDate in Table 1 and can perform a series of operations.

3. For mySession expiration, you can use a thread or window service to clean up table 1 every 20 minutes (if the CheckInDate in table 1 and the current time interval exceed 20 minutes, the user's data will be cleared)

 

 

The general process is almost like this. I think this should be able to simulate the 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.