diff --git a/controller/Session.class.php b/controller/Session.class.php index 04683d9d..5387dd2e 100644 --- a/controller/Session.class.php +++ b/controller/Session.class.php @@ -34,16 +34,17 @@ class Session static::setNamespace(static::NAMESPACE_DEFAULT, $oldSession); } - $site_visitor_id = key_exists(static::USER_ID, $_SESSION) ? $_SESSION[static::USER_ID] : ''; - $response = LBRY::logWebVisitor(static::SITE_ID, $site_visitor_id, $_SERVER['REMOTE_ADDR']); - if (!is_null($response) + Response::addPostRenderCallback(function(){ + $site_visitor_id = key_exists(static::USER_ID, $_SESSION) ? $_SESSION[static::USER_ID] : ''; + $response = LBRY::logWebVisitor(static::SITE_ID, $site_visitor_id, $_SERVER['REMOTE_ADDR']); + if (!is_null($response) && key_exists('data', $response) && key_exists('visitor_id', $response['data'])) { - $_SESSION[static::USER_ID] = $response['data']['visitor_id']; - } else { - $_SESSION[static::USER_ID] = ''; - } - + $_SESSION[static::USER_ID] = $response['data']['visitor_id']; + } else { + $_SESSION[static::USER_ID] = ''; + } + }); static::initFlashes(); } diff --git a/view/Response.class.php b/view/Response.class.php index 9b4526ba..f93e771b 100644 --- a/view/Response.class.php +++ b/view/Response.class.php @@ -35,6 +35,8 @@ class Response protected static $metaImages = []; protected static $facebookAnalyticsType = "PageView"; + private static $PostRenderCallbacks = array(); + public static function setMetaDescription($description) { static::$metaDescription = $description; @@ -369,6 +371,19 @@ class Response ); } + public static function addPostRenderCallback( $cb ) + { + array_push(static::$PostRenderCallbacks, $cb); + } + + public static function invokePostRenderCallbacks() + { + foreach(static::$PostRenderCallbacks as &$cb ) + { + $cb(); + } + } + // public static function addBodyCssClass($classOrClasses) // { diff --git a/web/index.php b/web/index.php index 4f06d8a7..2864c89c 100644 --- a/web/index.php +++ b/web/index.php @@ -28,6 +28,8 @@ try { View::compileCss(); } Controller::dispatch(Request::getRoutingUri()); + flush(); + Response::invokePostRenderCallbacks(); } catch (Throwable $e) { if (IS_PRODUCTION) { Slack::sendErrorIfProd($e);