We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10940
    • 71 Posts
    How do you automatically logout a user if they leave the privileged section of the site?

    Cheers,
    Global
      • 3749
      • 24,544 Posts
      If you have a different template for the "public" section, you could put a snippet in it that checks login status and logs the user out.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 10940
        • 71 Posts
        ok but how would that snippet look?
          • 3749
          • 24,544 Posts
          I’ve never done this, but I think it should work. You need to create a logout document that just contains this:

           [!WebLogin? &webloginmode=`lo`&logouthomeid=`1`!]


          That logs out the user and sends them to the home page (or wherever if you change &logouthomeid).

          Then, a call to this snippet goes in the template of the "public" pages:

          // Log out snippet
          $user_id = $modx->getLoginUserID();
          
          
          if ($user_id)) {  // somebody's logged in
          
               // you might want some exceptions for manager users here
          
          $go_url = $modx->makeUrl(##);  // replace ## with the doc id of the logout document.
          
          $modx->sendRedirect($go_url);
          
          }


          Another way to go would be to borrow the logout code from the weblogin snippet and log the user out directly using something like this:


            
          
          if ($modx->getLoginUserID())  {
          
                  $internalKey = $_SESSION['webInternalKey'];
                  $username = $_SESSION['webShortname'];
          
                  // invoke OnBeforeWebLogout event
                  $modx->invokeEvent("OnBeforeWebLogout",
                                          array(
                                              "userid"   => $internalKey,
                                              "username" => $username
                                          ));
          
                  // if we were launched from the manager 
                  // do NOT destroy session
                  if(isset($_SESSION['mgrValidated'])) {
                      unset($_SESSION['webShortname']);
                      unset($_SESSION['webFullname']);
                      unset($_SESSION['webEmail']);
                      unset($_SESSION['webValidated']);
                      unset($_SESSION['webInternalKey']);
                      unset($_SESSION['webValid']);
                      unset($_SESSION['webUser']);
                      unset($_SESSION['webFailedlogins']);
                      unset($_SESSION['webLastlogin']);
                      unset($_SESSION['webnrlogins']);
                      unset($_SESSION['webUsrConfigSet']);
                      unset($_SESSION['webUserGroupNames']);
                      unset($_SESSION['webDocgroups']);            
                  }
                  else {
                      // Unset all of the session variables.
          //            $_SESSION = array();
                      // destroy session cookie
                      if (isset($_COOKIE[session_name()])) {
                          setcookie(session_name(), '', 0, MODX_BASE_URL);
                      }
                      session_destroy();
          //            $sessionID = md5(date('d-m-Y H:i:s'));
          //            session_id($sessionID);
          //            startCMSSession();
          //            session_destroy();
                  }
          
                  // invoke OnWebLogout event
                  $modx->invokeEvent("OnWebLogout",
                                          array(
                                              "userid"        => $internalKey,
                                              "username"        => $username
                                          ));
          
                  // uncomment this to redirect to first authorized logout page 
             //     $url = $modx->makeURL($loHomeId);
              //    $modx->sendRedirect($url,0,'REDIRECT_REFRESH');
          }
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 10940
            • 71 Posts
            cheers thanks heaps!!!
              • 10940
              • 71 Posts
              I tried that and it didnt work first there was an extra ), which I cleaned up easily but then the browser said something about a loop that will never end. Im back to square one!
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                I suspect that you logged out from the home page (document 1) which would set up a loop of document 1 redirecting to document 1...

                Try making a weblink that goes to the home page, and use that as your logouthomeid.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 10940
                  • 71 Posts
                  For anyone interested this is how to log a member out automatically in a non-member area:
                  This is currently configured to only work for the home page.


                  Put the following snippet call before any code into the non-member area template:

                  [[logoutSnippet]]


                  Create a weblink with the external link as the home page of your site. i.e. http://www.yoursite.com/


                  Then create the following snippet titled logoutSnippet (There is probably way too much code in my snippet but I don’t have the time to clean it up!)

                  <?php
                  $type = isset($type) ? $type : 'simple';
                  	$regType = isset($regType) ? $regType : 'instant';
                  	$notify = isset($notify) ? $notify : '';
                  	$groups = isset($groups) ? $groups : '';
                  	$regRequired = isset($regRequired) ? $regRequired : '';
                  	$customTable = isset($customTable) ? $customTable : 'web_user_attributes_extended';
                  	$customFields = isset($customFields) ? $customFields : '';
                  	$prefixTable = isset($prefixTable) ? $prefixTable : 1;
                  	$lang = isset($lang) ? $lang : 'en';
                  	$userImageSettings = isset($userImage) ? $userImage : '105000,100,100';
                  	$dateFormat = isset($dateFormat) ? $dateFormat : '%A %B %d, %Y at %I:%M %p';
                  	$disableServices = isset($disableServices) ? explode(',', str_replace(', ',',',$disableServices)) : array();
                  	$tableCheck = isset($tableCheck) ? $tableCheck : 1;
                  	
                  	include_once MODX_BASE_PATH.'assets/snippets/webloginpe/webloginpe.class.php';
                  	include MODX_BASE_PATH.'assets/snippets/webloginpe/webloginpe.templates.php';
                  	if (file_exists(MODX_BASE_PATH.'assets/snippets/webloginpe/lang/'.$lang.'.php'))
                  	{
                  		include_once MODX_BASE_PATH.'assets/snippets/webloginpe/lang/'.$lang.'.php';
                  	}
                  	else
                  	{
                  		include_once MODX_BASE_PATH.'assets/snippets/webloginpe/lang/en.php';
                  		$modx->setPlaceholder('wlpe.message', $wlpe_lang[105]);
                  		print '[+wlpe.message+]';
                  	}
                  	
                  	$wlpe = new WebLoginPE($wlpe_lang, $dateFormat, $userImageSettings, $type);
                  	$wlpe->CustomTable($customTable, $customFields, $prefixTable, $tableCheck);
                  
                  	$liHomeId = isset($liHomeId) ? explode(',', $liHomeId) : '';
                  	$loHomeId = isset($loHomeId) ? $loHomeId : '';
                  	$regHomeId = isset($regHomeId) ? $regHomeId : '';
                  	$regSuccessId = isset($regSuccessId) ? $regSuccessId : '';
                  	$regSuccessPause = isset($regSuccessPause) ? $regSuccessPause : 5;
                  	$profileHomeId = isset($profileHomeId) ? $profileHomeId : '';
                  	$inputHandler = isset($inputHandler) ? explode('||', $inputHandler) : array();
                  	$usersList = isset($usersList) ? $usersList : '';
                  	
                  	if ($regType == 'verify'){$wlpeRegisterTpl = $wlpeRegisterVerifyTpl;}else{$wlpeRegisterTpl = $wlpeRegisterInstantTpl;}
                  	
                  	$displayLoginFormTpl = isset($loginFormTpl) ? $wlpe->Template($loginFormTpl) : $wlpeDefaultFormTpl;
                  	$displaySuccessTpl = isset($successTpl) ? $wlpe->Template($successTpl) : $wlpeDefaultSuccessTpl;
                  	$displayRegisterTpl = isset($registerTpl) ? $wlpe->Template($registerTpl) : $wlpeRegisterTpl;
                  	$displayRegSuccessTpl = isset($registerSuccessTpl) ? $wlpe->Template($registerSuccessTpl) : $wlpeDefaultFormTpl;
                  	$displayProfileTpl = isset($profileTpl) ? $wlpe->Template($profileTpl) : $wlpeProfileTpl;
                  	$displayViewProfileTpl = isset($viewProfileTpl) ? $wlpe->Template($viewProfileTpl) : $wlpeViewProfileTpl;
                  	$displayUsersOuterTpl = isset($usersOuterTpl) ? $wlpe->Template($usersOuterTpl) : $wlpeUsersOuterTpl;
                  	$displayUsersTpl = isset($usersTpl) ? $wlpe->Template($usersTpl) : $wlpeUsersTpl;
                  	$displayManageOuterTpl = isset($manageOuterTpl) ? $wlpe->Template($manageOuterTpl) : $wlpeUsersOuterTpl;
                  	$displayManageTpl = isset($manageTpl) ? $wlpe->Template($manageTpl) : $wlpeManageTpl;
                  	$displayManageProfileTpl = isset($manageProfileTpl) ? $wlpe->Template($manageProfileTpl) : $wlpeManageProfileTpl;
                  	$displayManageDeleteTpl = isset($manageDeleteTpl) ? $wlpe->Template($manageDeleteTpl) : $wlpeManageDeleteTpl;
                  	$displayProfileDeleteTpl = isset($profileDeleteTpl) ? $wlpe->Template($profileDeleteTpl) : $wlpeProfileDeleteTpl;
                  	$displayActivateTpl = isset($activateTpl) ? $wlpe->Template($activateTpl) : $wlpeActivateTpl;
                  	$displayResetTpl = isset($resetTpl) ? $wlpe->Template($resetTpl) : $wlpeResetTpl;
                  	$notifyTpl = isset($notifyTpl) ? $wlpe->Template($notifyTpl) : $wlpeNotifyTpl;
                  	$notifySubject = isset($notifySubject) ? $notifySubject : 'New Web User for '.$modx->config['site_name'].'.';
                  	$messageTpl = isset($messageTpl) ? $wlpe->Template($messageTpl) : $wlpeMessageTpl;
                  	$tosChunk = isset($tosChunk) ? $wlpe->Template($tosChunk) : $wlpeTos;
                  	$modx->setPlaceholder('tos', $tosChunk);
                  	
                  	$loadJquery = isset($loadJquery) ? $loadJquery : false;
                  	$customJs = isset($customJs) ? $customJs : '';
                  
                  $user_id = $modx->getLoginUserID();
                  
                  if ($user_id) { 
                   // somebody's logged in
                  
                  $wlpe->Logout($type, $loHomeId);
                  return $displayLoginFormTpl;
                  
                  
                  $go_url = $modx->makeUrl(78);
                  $modx->sendRedirect($go_url); 
                  }
                  ?>
                  


                  Notice the third last line:
                  $go_url = $modx->makeUrl(78);

                  78 was the page ID number for my weblink. PLace your weblink ID here,


                  Hope this helps anyone, its a pretty messy solution really but Im still learning
                  Regards,
                  Global
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    You could also use the MemberCheck snippet and have the chunk for logged-in users contain a webLogin snippet call; if the member is logged in only the logout link will be displayed, and of course only logged-in users will see it.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org