Last active
August 4, 2021 08:02
-
-
Save pgooood/3cce10f2fde1edcbe02f95ccbf1fc7c4 to your computer and use it in GitHub Desktop.
Битрикс: кроссавторизация на поддоменах на одном ядре
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ставим авторизованную сессионную куку для родительского | |
* домена, чтобы все поддомены ее видели | |
*/ | |
AddEventHandler('main','OnAfterUserAuthorize',function($arUser){ | |
$m = null; | |
if(preg_match('/^.+(\.mysite\.com)$/',$_SERVER['SERVER_NAME'],$m)) | |
setcookie(session_name(),session_id(),0,'/',$m[1]); | |
}); | |
/** | |
* удаляем сессионную куку, если есть вторая от родительского домена | |
*/ | |
AddEventHandler('main','OnBeforeProlog',function(){ | |
if(!$GLOBALS['USER']::IsAuthorized() | |
&& ($arHeaders = getallheaders()) | |
&& ($arCookies = explode(';',$arHeaders['Cookie'])) | |
){ | |
$m = null; | |
foreach($arCookies as $str) | |
if(preg_match('/^\s*'.session_name().'=(.*)$/',$str,$m) | |
&& $m[1] != session_id() | |
){ | |
unset($_COOKIE[session_name()]); | |
setcookie(session_name(),null,-1,'/'); | |
header('Refresh:0'); | |
die; | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment