Home  >  Article  >  Backend Development  >  Detailed explanation of how to enable Django applications with different ports on the same server to be opened in the same browser

Detailed explanation of how to enable Django applications with different ports on the same server to be opened in the same browser

高洛峰
高洛峰Original
2017-03-08 10:06:041289browse

If we have two Django applications site1 and site2 running on different ports of the same server at the same time, and we log in from different tabs of the same browser. Then this happens. When we log in to site2, the users logged in on site1 will be kicked out.

Why does this happen? This is related to Django's session framework. Here is a brief introduction: When we visit a Django website for the first time, Django will generate a session to save some information about the current session. At the same time, a hash value session_key will be generated and a cookie will be generated and sent to the client. The name of this cookie is set according to the SESSION_COOKIE_NAME in the setting. The default is "sessionid" (emphasis added). In this way, the session_key will be sent to the server along with the cookie next time. The server searches for the corresponding session object based on session_key and obtains the information of the current session, including login information of course.

So there is only one truth to the above situation (Conan pushes the face with glasses):

  1. When we log in to site1, we get a cookie called sessionid, which stores session_key1.

  2. When we log in to site2, the cookie called sessionid will be updated. Now its value is session_key2 (the browser stores cookies based on IP instead of port, so the same name will be updated. cookies).

  3. So now when we use the new session_key to access site1, we will not be able to get the original login information, and we need to log in again.

So how to solve it? After understanding the above mechanism, you only need to set SESSION_COOKIE_NAME in the setting. For example, you can set SESSION_COOKIE_NAME = ‘site2’ in site2, and use the default in site1. Of course, site1 and site2 can also be set at the same time.

For more detailed explanations on how to enable Django applications with different ports on the same server to be opened in the same browser, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn