Home > Backend Development > PHP Tutorial > Detailed explanation of the steps to implement single sign-on in Yii2

Detailed explanation of the steps to implement single sign-on in Yii2

php中世界最好的语言
Release: 2023-03-26 15:20:02
Original
2395 people have browsed it

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(&#39;.&#39;, $_SERVER["HTTP_HOST"]);
if (count($host) > 2) {
  define(&#39;DOMAIN&#39;, $host[1] . &#39;.&#39; . $host[2]);
} else {
  define(&#39;DOMAIN&#39;, $host[0] . &#39;.&#39; . $host[1]);
}
Copy after login

2. Add

<?php
&#39;user&#39; => [
  &#39;identityClass&#39; => &#39;common\models\User&#39;,
  &#39;enableAutoLogin&#39; => true,
  &#39;identityCookie&#39; => [&#39;name&#39; => &#39;_identity&#39;, &#39;httpOnly&#39; => true, &#39;domain&#39; => &#39;.&#39;.DOMAIN],
],
&#39;session&#39; => [
  &#39;cookieParams&#39; => [&#39;domain&#39; => &#39;.&#39;.DOMAIN, &#39;lifetime&#39; => 0],
  &#39;timeout&#39; => 3600,
],
Copy after login

in the components configuration of config. 3. Use

<?php
//设置
Yii::$app->session[&#39;var&#39;]=&#39;value&#39;;
//使用
echo Yii::$app->session[&#39;var&#39;];
//移除
unset(Yii::$app->session[&#39;var&#39;]);
Copy after login
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!

Related labels:
source:php.cn
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