Home > Article > WeChat Applet > Introduction to WeChat Development (10) Login Status Maintenance
》What is login state?
The so-called login state means that when the program is running, it can identify the current user and prove its uniqueness and legality.
We know that the WEB server obtains session through the cookie carried by the browser to determine whether it is the same user (or browser); the Restful service uses the client The unique ID is passed from the end to identify the calling user.
Applications with their own user systems
User sensitive data is only visible to users
Involving user-related businesses, such as my orders, personalized recommendations, etc.
Through wx. login gets the login status.
The code returned by logging in is sent to the server, and the server exchanges the code for session_key and openid.
After obtaining the session_key, the cache framework is generally used to save the login status. The server randomly generates a string of unique strings. 3rdSessionId is the key, and session_key is the value to form a key-value pair and store it in the cache. The cache time It is at your discretion depending on the situation.
Return 3rdSessionId to the client
The client caches the 3rdSessionId into localStorage, and subsequent interfaces read the 3rdSessionId from the cache and pass it to Server; the server determines the user's identity based on 3rdSessionId.
If the server searches for the existence of session_key in the cache based on the 3rdSessionId, if it exists, it will be executed normally; if it does not exist and the applet is not logged in, go back to the first step of the process.
The above process is the officially recommended process and is not the only way to maintain the login status.
In addition, you can also use wx.checkSession() in the mini program to check whether the login status has expired. If it expires, call the wx.login interface again.
session_key is valid for 30 days on the WeChat server. It is recommended that the server cache the session_key for no more than 30 days.
【Related recommendations】
1. WeChat public account platform source code download
3. WeChat People Network v3.4.5 Advanced Commercial Edition WeChat Rubik’s Cube Source Code
The above is the detailed content of Introduction to WeChat Development (10) Login Status Maintenance. For more information, please follow other related articles on the PHP Chinese website!