We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4750
    • 28 Posts
    I am trying to find a final way of logging into modx using php to log someone in. I have found this in the forum but it’s not working are there variables that have to be filled in? It would be good to have a final solution.

    <?php
    $loginContext = isset($_REQUEST[’login_context’]) && !empty($_REQUEST[’login_context’]) ? $_REQUEST[’login_context’] : $modx->context->get(’key’);
    $contexts = !empty($scriptProperties[’contexts’]) ? $scriptProperties[’contexts’] : ’’;
    $contexts = !empty($_REQUEST[’add_contexts’]) ? $_REQUEST[’add_contexts’] : $contexts;
    $authenticated = $modx->user->isAuthenticated($loginContext);

    if (!$authenticated) {
    // get the username and password in a way that fits your snippet
    $_POST[’username’] = $username;
    $_POST[’password’] = $password;

    if (!isset($_POST[’login_context’])) $_POST[’login_context’] = $loginContext;
    $response = $modx->executeProcessor(array(
    ’action’ => ’login’,
    ’location’ => ’security’,
    ’login_context’ => $loginContext,
    ’add_contexts’ => $contexts,
    ’rememberme’ => !empty($_REQUEST[’rememberme’]) ? true : false,
    ));
    if (empty($response[’success’]) ) {
    // code to handle unhappy user
    } else {
    // code to handle happy user
    }
    }