Home > Article > Backend Development > Detailed explanation of the steps to implement single sign-on in Yii2
This time I will bring you a detailed explanation of the steps to implement single sign-in in Yii2. What are the precautions for Yii2 to implement single sign-in. The following is a practical case, let’s take a look.
Modify/common/config/main.php
1. Add the following code to the config header
<?php // Session 跨域 $host = explode('.', $_SERVER["HTTP_HOST"]); if (count($host) > 2) { define('DOMAIN', $host[1] . '.' . $host[2]); } else { define('DOMAIN', $host[0] . '.' . $host[1]); }
2. Add
<?php 'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, 'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => '.'.DOMAIN], ], 'session' => [ 'cookieParams' => ['domain' => '.'.DOMAIN, 'lifetime' => 0], 'timeout' => 3600, ],
in the components configuration of config. 3. Use
<?php //设置 Yii::$app->session['var']='value'; //使用 echo Yii::$app->session['var']; //移除 unset(Yii::$app->session['var']);in the controller.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
A brief discussion on how to use pack and unpack in PHP
How to reset the array in PHP Index consecutive numbers
The above is the detailed content of Detailed explanation of the steps to implement single sign-on in Yii2. For more information, please follow other related articles on the PHP Chinese website!