Elegantly maintain user session and status when switching AnQiCMS language sites
Today, let's delve into how AnQiCMS responds to this challenge and provide some practical strategies to ensure that your multilingual site maintains user state consistency when switching languages.
Understand session and state information of multilingual websites
Firstly, we need to clarify the meaning of "session" and "status information" in a multilingual environment.A session typically refers to the interactive state maintained by a user during a series of activities on a website, the most common being the login state.After the user logs in, the system generates a session ID, which is stored in the user's browser through means such as Cookie, and the server identifies the user based on this ID.
“Status information” is more general, including users' language preferences, shopping cart contents, browsing history, form filling progress, and even customized page layouts, etc.This information may be dynamically generated and updated throughout the user's visit, which is crucial for providing a personalized experience.
In AnQiCMS, the core of multilingual support lies in the ability to 'switch and display multilingual content', which means the system needs to provide corresponding content versions for different languages.In order to achieve seamless switching of session and state information, it requires deeper technical considerations.
How AnQiCMS keeps user sessions through Cookie
AnQiCMS is a high-performance content management system based on the Go language, whose session management usually follows Web standards and mainly relies on HTTP cookies to maintain user status.After the user successfully logs into a website driven by AnQiCMS, the server will send a Cookie containing the session ID to the user's browser.The key to this Cookie lies in its "Domain" and "Path" settings.
Multilingual switching based on path (for example:
yourdomain.com/enandyourdomain.com/fr)In this mode, content in different languages is distinguished by different paths in the URL. For example, English content is in/enthe path, French content is in/frunder the path. If the session Cookie of AnQiCMS is set to act on the root path of the main domain (/), then when the user switches from/ento/frWhen, the browser session Cookie will still be sent to the server with the request.AnQiCMS server only needs to read this unchanging session cookie to identify the user's login status, thereby maintaining the continuity of the session.This is the simplest and most common case, usually no additional configuration is required.Based on subdomain-based multilingual switching (for example:
en.yourdomain.comandfr.yourdomain.com)When content in different languages is distinguished by different subdomains, the configuration of session cookies becomes particularly important.The browser defaults to restricting Cookies to the subdomain it was set in.en.yourdomain.comLogin, its Cookie may be set to only been.yourdomain.comValid, switch tofr.yourdomain.comAfter that, it may be considered as not logged in.}To solve this problem, you need to set the session Cookie's
Domainattribute to the parent domain (for example.yourdomain.com. Note the dot at the beginning). This way, all subdomains (includingen.yourdomain.comandfr.yourdomain.com) Can read and share this Cookie. In the deployment environment of AnQiCMS, this is usually configured through Nginx or Apache and other reverse proxy servers, setting or modifying the Cookie during request forwarding.DomainProperty. AnQiCMS itself as a backend service, receives these requests processed by the proxy server and correctly maintains the session.
3.