Skip to content

Instantly share code, notes, and snippets.

@pgooood
Last active August 4, 2021 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgooood/3cce10f2fde1edcbe02f95ccbf1fc7c4 to your computer and use it in GitHub Desktop.
Save pgooood/3cce10f2fde1edcbe02f95ccbf1fc7c4 to your computer and use it in GitHub Desktop.
Битрикс: кроссавторизация на поддоменах на одном ядре
<?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