Cookie and session

Source: Internet
Author: User

Cookie and session should have a more accurate and detailed description in the HTTP protocol. Here I will summarize my understanding based on the information I usually see as follows:

1. Cookies are stored on client computers and have attributes such as name, domain, and expire.

2. For security reasons, cross-origin Cookies cannot be obtained. For example, a.com cookies can only be read by a.com, x.a.com, y.a.com, and B .com.

3. The cookie has a validity period. Generally, it is disabled by the browser by default. to delete the cookie, you can simply set the validity period to yesterday.

4. Each time the client sends a request to the server, it automatically carries a cookie. Therefore, the cookie size cannot exceed 4 kb. The server usually sends a cookie only in response to the initial request from the client, this should be sent only when the program controls writing the cookie, otherwise the response will not carry the cookie. Because cookies are mainly used to indicate some information of client users, while HTTP is stateless, the server cannot identify its historical conversation information if the client does not contain cookies.

5. You can use URL parameters or other methods to disable cookies on Some browsers.

6. sessions are stored on the server and client session information. In fact, they store key-value pairs like cookies.

7. Since HTTP is stateless, how does the server correspond the session to the client? The assumption here may be that the server generates a session for the initial client request and tells the client the sessionid. This sessionid is automatically carried by the client every time the client uses cookies or other methods, then the server segment will know which session it corresponds. In addition, session objects on the server can store a large amount of content.

8. Security issues may occur when sessionid is intercepted. For example, another client can carry the sessionid to send a request to simulate login. In this case, we usually need to use SSL to solve the security problem. If there is no SSL mechanism, the simple method is to store the client's IP address, http_user_agent, and other information in the initial stage. Each request is verified. Of course, this is still not very safe.

9. To prevent hackers from automatically constructing a large number of sessionids to imitate other users, you can call the regenerate method during each session initialization:

<? PHP
Session_start ();
If (! Isset ($ _ session ['initiation']) // whether the first request from a specific terminal is identified here, because it is impossible to generate an ID every time a request is made.
{
Session_regenerate_id ();
$ _ Session ['initiation'] = 1;
}
If (! Isset ($ _ session ['Count']) $ _ session ['Count'] = 0;
Else ++ $ _ session ['Count'];
Echo $ _ session ['Count'];
?>

10. There should be at least two types of sessionid transmission, one based on cookie, and the other based on get methods, such ....? Phpsessionid = 2321

You can use ini_set ('session. use_only_cookies ', 1); to force sessionid to be transmitted Based on cookies, the get method will become invalid.

 

My personal suggestions are as follows:

1. When the server receives a client request, it first checks from the session to determine whether the current client has logged on to the user, for example, whether the session ['user'] exists.

2. If the session does not exist, it indicates that the client has not logged in, the session has expired, or is a new session, the cookie value in the client request is read to see if the user name and password are stored, it is verified and stored in session ['user.

3. If the cookie in the user request does not contain the user name and password information, it is likely that this user is logged on for the first time or the client cookie expires, it will be in post or get (of course, it is generally post) to check whether there is a user name and password value. If the verification is successful, set the session ['user'] value. You can also generate cookie information containing the user name and password as required, set the expiration time, for example, one day, one week, always. If the browser is closed, it will become invalid. In my opinion, you do not need to set the cookie because you have a session.

4. From this client, if the browser page is not closed (the entire browser is not closed by default in IE8, the same session will be shared ), the sessionid is automatically carried by the cookie or get method each time a request is sent. Then, the service gas end can know from step 1 that the user has logged in. If the browser page is closed, if the cookie is still valid after it is re-opened, it will be processed through step 2. Otherwise, process it in step 3.

Question? Cookie can be set to the expiration time, So when will the server session be clear? Because the server session is only managed on the service gas end, the server does not know whether the client has completed the session. We can perform this operation when the user clicks the "exit" button, or when you close a page, there is a problem when you close the page, that is, the user may have opened multiple pages. You cannot close one of these pages to clear the session information. However, if it is not cleared, the last session of the user is still in progress. After the user logs on again, a new session is generated. Can I store the number of pages opened by the current site in the client cookie? When the last page is closed, a request is sent to the server to clear 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.