We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45892
    • 9 Posts
    Why is the following code inconsistently successful at logging in?

    define('MODX_CORE_PATH', '/path/to/core/');
    define('MODX_CONFIG_KEY', 'config');
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    
    $modx = new modX();
    $modx -> setDebug(TRUE);
    $modx -> initialize('web');
    $modx->getService('error','error.modError');
    
    $response = $modx->runProcessor('security/login', array( 'username'=>'un','password'=>'pw'));


    This code is used in an external script. It seems pretty standard, but for me it yields inconsistent results. The only pattern I see is when the user logs out on the website; after that the above script will work for a time. Eventually something will occur that then keeps the script from logging the user in successfully.

    It doesn't seem to make a difference whether the user is logged in or out on the website or which webpage is visited. However, there must be something triggering this issue. Anyone have any ideas on what that trigger might be?

    For clarification:

    • When the code is unsuccessful, $modx->user->get('username') shows '(anonymous)' as the user.
    • When the script is unsuccessful it is consistently unsuccessful until the user is logged out on the website (which sometimes requires logging in first).
    Edit: It looks like a change of login state fixes (resets) the issue. If I'm logged out on the website and the issue appears, just logging in on the website fixes it.


    [ed. note: shanderson last edited this post 10 years, 6 months ago.]