We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15566
    • 73 Posts
    Following this thread* (http://forums.modx.com/thread/82028/allowing-member-access-to-multiple-contexts) I am writing my own script to log some body in programatically. I will have their username (and they with be authenticated), but I won't have their password. How can I log somebody in using just their username? Also, if possible, how can I log out somebody across all contexts?

    * I thought it best to start a new thread rather than go off on a tangent on the context question.
      • 3749
      • 24,544 Posts
      /* This is assuming that you have the username and are *sure* it's a legit user */
      $usr = $modx->getObject('modUser', array('username' => $username);
      $modx->user =& $usr;
      $modx->getUser();
      /* optionally send contexts to log the user in to as a comma-separated list
         in the &contexts property -- falls back to current context (e.g., 'web') */
      $contexts = $modx->getOption('contexts', $scriptProperties, $modx->context->get('key'));
      $contexts = explode(',', $contexts);
      foreach ($contexts as $ctx) {
          $modx->user->addSessionContext($ctx);
      }


      If you know it's the web context, you can skip the foreach and just use:

      $modx->user->addSessionContext('web');


      If you want to send the user somewhere after logging them in:

      $docId = '12'; /* ID of the page you want to send them to */
      $url = $modx->makeUrl($id, "", "", "full");
      $modx->sendRedirect($url);
        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