Home  >  Article  >  WeChat Applet  >  Introduction to WeChat Development (10) Login Status Maintenance

Introduction to WeChat Development (10) Login Status Maintenance

零下一度
零下一度Original
2017-05-24 09:58:162040browse

》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.
Introduction to WeChat Development (10) Login Status Maintenance

》Why do you need to maintain the login status?

  • 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.

》mini program login state maintenance implementation process

  1. Through wx. login gets the login status.

  2. The code returned by logging in is sent to the server, and the server exchanges the code for session_key and openid.

  3. 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.

  4. Return 3rdSessionId to the client

  5. 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.

  6. 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.

Introduction to WeChat Development (10) Login Status Maintenance

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

2. 小 Pigcms (PigCms) micro-e-commerce System operation version (independent Weidian mall + three-level distribution system)

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!

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