Home > WeChat Applet > Mini Program Development > How to manage sessions in mini programs?

How to manage sessions in mini programs?

青灯夜游
Release: 2020-05-16 18:01:02
forward
3235 people have browsed it

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();
Copy after login

Return the sessionId

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

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;
Copy after login

Code in app.js

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

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){
  }
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template