欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Yii2 中实现单点登录的方法

程序员文章站 2022-04-22 08:51:42
本文介绍了yii2 中实现单点登录的方法,分享给大家,具体如下: 修改 /common/config/main.php 一、在 config 头部上加上以下代码...

本文介绍了yii2 中实现单点登录的方法,分享给大家,具体如下:

修改 /common/config/main.php

一、在 config 头部上加上以下代码

<?php
// session 跨域
$host = explode('.', $_server["http_host"]);
if (count($host) > 2) {
  define('domain', $host[1] . '.' . $host[2]);
} else {
  define('domain', $host[0] . '.' . $host[1]);
}

二、在 config 的 components 配置中加入

<?php
'user' => [
  'identityclass' => 'common\models\user',
  'enableautologin' => true,
  'identitycookie' => ['name' => '_identity', 'httponly' => true, 'domain' => '.'.domain],
],
'session' => [
  'cookieparams' => ['domain' => '.'.domain, 'lifetime' => 0],
  'timeout' => 3600,
],

三、controller 中使用

<?php
//设置
yii::$app->session['var']='value';
//使用
echo yii::$app->session['var'];
//移除
unset(yii::$app->session['var']);

四、测试

4.1 www.aaa.com 登陆

4.2 www.bbb.com session 依然有效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。