Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

A thorough resolution of the program does not trigger the SESSION problem


May 30, 2021 Article blog



First, find the address where the first network request was initiated, and return the server to set-cookie when the global variable is stored

wx.request({
  ......
  success: function(res) {
    console.log(res.header);
    //set-cookie:PHPSESSID=ic4vj84aaavqgb800k82etisu0; path=/; domain=.fengkui.net

    // 登录成功,获取第一次的sessionid,存储起来
    // 注意:Set-Cookie(开发者工具中调试全部小写)(远程调试和线上首字母大写)
    wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
  }
})

2. Take sessionid into the header of request on request and pass it to the server on request, which can be obtained directly from the cookie

wx.request({
  ......
  header: {
    'content-type': 'application/json', // 默认值
    'cookie': wx.getStorageSync("sessionid")
    //读取sessionid,当作cookie传入后台将PHPSESSID做session_id使用
  },
  success: function(res) {
    console.log(res)
  }
})

Third, background access to PHPSESSID in cookie PHPSESSID as session_id use

<?php
// 判断$_COOKIE['PHPSESSID']是否存在,存在则作session_id使用
if ($_COOKIE['PHPSESSID']) {
    session_id($_COOKIE['PHPSESSID']);
}

session_start();
echo session_id();

 A thorough resolution of the program does not trigger the SESSION problem1

Recommended good lessons: WeChat small program micro-class, WeChat small program mall - 3 days to learn interface design