【 ThinkPHP 5 】 session 跨域问题解决

时间:2024-03-16 12:11:00

具体如源码所示,黄色背景显示:

 1     /**
 2      * session初始化
 3      * @param array $config
 4      * @return void
 5      * @throws \think\Exception
 6      */
 7     public static function init(array $config = [])
 8     {
 9         if (empty($config)) {
10             $config = Config::get('session');
11         }
12         // 记录初始化信息
13         App::$debug && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info');
14         $isDoStart = false;
15         if (isset($config['use_trans_sid'])) {
16             ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
17         }
18 
19         // 启动session
20         if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
21             ini_set('session.auto_start', 0);
22             $isDoStart = true;
23         }
24 
25         if (isset($config['prefix']) && ('' === self::$prefix || null === self::$prefix)) {
26             self::$prefix = $config['prefix'];
27         }
28         if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
29             session_id($_REQUEST[$config['var_session_id']]);
30         } elseif (isset($config['id']) && !empty($config['id'])) {
31             session_id($config['id']);
32         }
33         if (isset($config['name'])) {
34             session_name($config['name']);
35         }
36         if (isset($config['path'])) {
37             session_save_path($config['path']);
38         }
39         if (isset($config['domain'])) {
40             ini_set('session.cookie_domain', $config['domain']);
41         }
42         if (isset($config['expire'])) {
43             ini_set('session.gc_maxlifetime', $config['expire']);
44             ini_set('session.cookie_lifetime', $config['expire']);
45         }
46         if (isset($config['secure'])) {
47             ini_set('session.cookie_secure', $config['secure']);
48         }
49         if (isset($config['httponly'])) {
50             ini_set('session.cookie_httponly', $config['httponly']);
51         }
52         if (isset($config['use_cookies'])) {
53             ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
54         }
55         if (isset($config['cache_limiter'])) {
56             session_cache_limiter($config['cache_limiter']);
57         }
58         if (isset($config['cache_expire'])) {
59             session_cache_expire($config['cache_expire']);
60         }
61         if (!empty($config['type'])) {
62             // 读取session驱动
63             $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
64 
65             // 检查驱动类
66             if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
67                 throw new ClassNotFoundException('error session handler:' . $class, $class);
68             }
69         }
70         if ($isDoStart) {
71             session_start();
72             self::$init = true;
73         } else {
74             self::$init = false;
75         }
76     }

 所以,只需要config.php中这样配置即可,亲测可用

【 ThinkPHP 5 】 session 跨域问题解决