search
HomeWeChat AppletMini Program DevelopmentHow to manage sessions in mini programs?

How to manage sessions in mini programs?

May 16, 2020 pm 06:01 PM
sessionApplets

How to manage sessions in mini programs? The following article will introduce to you a tutorial on session management for WeChat applet development. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to manage sessions in mini programs?

In the development of WeChat applet, each request initiated by wx.request() is a different session for the server, and the WeChat applet will not Bring the session information back to the server, that is, corresponding to different sessions on the server. Since the session is used in the project to save user information, subsequent requests are equivalent to not logging in.

Note that the session here is not the session maintained by the mini program through the wx.login() method, but our own server-side session.

Because under normal circumstances, when the client initiates a request to the server, the session information is stored in the cookie and brought back to the server in the form of a request header, and the specific information in the request header is the session id. As shown in the figure below

How to manage sessions in mini programs?

#The red one is that we need to carry the request header returned to the server when making the request, so what we need is the value of JESSIONID. It just so happens that the WeChat applet also provides support for request headers.

Solution:

1. When the user logs in, the server returns the user's sessionId.

2. The applet saves the sessionId and carries the sessionId in the request header in each subsequent request

Key code of the server:

How to get the sessionId, you can easily get it as long as you get the request object

Sting sessionId = request.getSession().getId();

Return the sessionId

...//登录时的业务代码
response.getWriter.write(sessionId);//把sessionId返回给前台

After logging in to the applet, store the sessionId in the global variable, app.js It's a good place to store global variables.

...//登录后的逻辑处理
getApp().globalData.header.Cookie = \'JSESSIONID=\' + _data.sessionId;

Code in app.js

globalData:{
header:{\'Cookie\': \'\'} //这里还可以加入其它需要的请求头,比如\'x-requested-with\': \'XMLHttpRequest\'表示ajax提交,微信的请求时不会带上这个的
},

Bring sessionId when requesting the applet

var header = getApp().globalData.header; //获取app.js中的请求头
wx.request({
url: localhost:8080/xx/xx,
header: header, //请求时带上这个请求头
success:function(res){
  }
}

It is recommended to encapsulate your own request information and process some common logic.

Recommendation: " Mini Program Development Tutorial"

The above is the detailed content of How to manage sessions in mini programs?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use