Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. Cookies and sessions are used to track the browser user identity. Cookie Mechanism Cookie is a set of data stored on the client after the browser (UserAgent) accesses some websites. It is used for tracking users such as websites to implement user-defined functions...
Cookies and sessions are used to track browser user identities.
Cookie Mechanism
Cookies are a set of data stored on the client after the browser (User Agent) accesses some websites. These websites are used for tracking users such as websites to implement User-defined functions.
The Domain and Path attributes of the Cookie identify the website that sent the Cookie to the browser. The Expires attribute of the Cookie identifies the Cookie validity period. When the Cookie validity period Expires, the data is automatically deleted.
If no expiration time is set, it indicates that the life cycle of the Cookie is the browser session. If the browser window is closed, the Cookie disappears. This Cookie is called a session Cookie. Session cookies are generally stored in memory instead of hard disks. If the expiration time is set, the browser will save the Cookie to the hard disk, and then open the browser again. These cookies are still valid until the preset expiration time is exceeded. Cookies stored on hard disks can be shared among different browser processes, such as two IE Windows. For Cookies stored in the memory, different browsers have different processing methods.
Session Mechanism
Session is stored on the server side similar to the HashTable structure (the implementation of each Web development technology may be different, which is directly called HashTable) to store user data. When the browser sends a request for the first time, the server automatically generates a HashTable and a Session ID to uniquely identify the HashTable and send it to the browser through response. When the browser sends the second request, it places the Session ID in the previous Server Response in the request and sends it to the server. The server extracts the Session ID from the request, and compare it with all the saved Session IDs to find the HashTable corresponding to this user.
In general, the server will save this HashTable within a certain period of time (20 minutes by default). After the time limit is reached, the HashTable will be destroyed. Before destruction, the programmer can temporarily store some user data in the HashTable in the form of Key and Value. Of course, some databases are also used to serialize and save this HashTable. The advantage is that there is no time limit, and the disadvantage is that the database will expand rapidly as time increases, especially when traffic increases. Generally, the previous method is used to reduce the pressure on the server.
Client implementation form of the Session (that is, the method for saving the Session ID)
Generally, browsers provide two methods for saving, and a programmer can use HTML to hide fields for custom implementation:
[1] using cookies for saving. This is the most common method. The implementation of the "remember my logon status" function in this article is based on this method. The server sends the Session ID to the browser by setting the Cookie. If we do not set the expiration time, the Cookie will not be stored on the hard disk. When the browser is closed, the Cookie will disappear and the Session ID will be lost. If we set this time to several days later, the Cookie will be stored in the client's hard disk. Even if the browser is closed, this value will still exist. The next time you visit the corresponding website, the same sample will be sent to the server.
[2] How to append URL information, that is, as we often see that the JSP website has aaa. jsp? JSESSIONID =. This method is the same as setting the Cookie expiration time in the first method.
[3] The third method is to add hidden fields to the page form. This method is actually the same as the second method, except that the former sends data through GET and the latter uses POST to send data. But the latter is obviously troublesome.
Differences between cookie and session:
Cookie data is stored on the client, and session data is stored on the server.
Simply put, when you log on to a website, if the web server uses a session, all the data is stored on the server, each time the client requests the server, it sends the sessionid of the current session. The server determines whether the user is logged on or has certain permissions based on the current sessionid. Because the data is stored on the server, you cannot forge it. However, if you can obtain the sessionid of a logon user, using a special browser to forge the user's request is successful. Sessionid is randomly allocated when the server is connected to the client. Generally, there will be no duplicates. However, if there are a large number of concurrent requests, it is not possible to be repeated. I have met it once. Log on to a website and start to display your own information. When a period of time has elapsed, a refresh will display others' information.
If the browser uses cookies, all data is stored on the browser. For example, after you log on to the server and set the cookie username, when you request the server again, the browser will send the username to the server. These variables are marked with special characters. The server is interpreted as a cookie variable. So as long as the browser is not closed, the cookie variable will always be valid, so it can ensure that the line is not dropped for a long time. If you can intercept a user's cookie variable and then forge a data packet to send it, the server still thinks that you are legal. Therefore, cookie attacks are more likely. If the validity period is set, the cookie is saved on the client's hard disk. When you access the website again, the browser checks whether there is any cookie, read the cookie and send it to the server. If you save a forum cookie on your machine, it will be valid for one year. If someone intrude into your machine, copy your cookie and put it under the directory of his browser, then he logs on to the website as you. Therefore, cookies can be forged. Of course, the idea is required to copy the cookie file directly to the cookie directory. the browser does not recognize it and has an index. the dat file stores the creation time of the cookie file and whether the file has been modified. Therefore, you must first have the cookie file for the website and cheat the browser from the guaranteed time, I once tried it on the school's vbb forum and copied others' cookies to log on. I posted a post in the name of someone else. There is no problem at all.
A more effective method is to define a browser, which we call a hacker browser. For example, we can modify the firefox browser, customize the sessionid, and customize the cookie value. Network security will face severe challenges.
Both of them can be used to store private things, and both have validity periods. The difference is:
The session is placed on the server. The expiration time depends on the setting of the service period,
The cookie exists on the client. If it expires, it can be set when the cookie is generated.
1. cookie data is stored in the client's browser, and session data is stored on the server.
2. Cookies are not very secure. Others can analyze the Cookies stored locally and perform cookie spoofing. For security reasons, session
3. The session will be stored on the server for a certain period of time. When the number of accesses increases, the performance of your server will be occupied. To reduce the server performance, you should use cookies.
4. the maximum size of a single cookie on the client is 3 kb. That is to say, a website cannot store 3 kb of cookies on the client.
5. I have not heard of any restrictions on 300
6. Personal suggestions:
Store important information such as login information as SESSION, and store other information in cookies if necessary.
Implement the "remember my logon status" Function
We have learned that if we set the expiration time of a Session ID to one year when it is sent to the client through Cookie, in the next year, when the client accesses my website, it sends the Session ID Value back to the server. The server restores the Hashtable for storing the Key-Value pair from the memory or database based on the Session ID.
Our functions have been well implemented. However, as mentioned above, the Session will not always exist. after a certain period of time, the Session on the server will be sold out to reduce the access pressure on the server. After the data on the server is destroyed, even if the client stores cookies, there is no way to "remember my logon status.
The common solution is to store the user name and the encrypted password on the client through cookies. After the Session on the server is destroyed, use the username stored in the Cookie and the encrypted password to re-execute the logon operation, recreate the Session, and update the Session ID stored in the Cookie on the client, this operation is behind the user's request for a page resource that requires authentication. It is transparent to the user, so it achieves the goal of "Remembering my logon status.
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB