We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36926
    • 701 Posts
    I'm trying to log a front end user into modx via and external script containing the login processor.

    I've got no issues with the runProcessor as the file logs in fine if I view the file directly within a browser, but there seems to be an issue when the file is loaded via curl.

    This file is used from magento using curl and it seems to login the user in, in terms of returning a successful response. I'm getting
    ["success"]=>
      bool(true)
      ["message"]=>
      string(0) ""
      ["total"]=>
      int(0)
      ["errors"]=>
      array(0) {
      }
      ["object"]=>
      array(2) {
        ["url"]=>
        string(0) ""
        ["token"]=>
        string(52) "modx57516511372036.00159532_257683f2492d6d2.48933569"
      }```
    


    And even within the manager the users login count goes up.

    But if I viewing protect pages it's as if your not logged in. It doesn't look like the correct session ID is getting set within the modx_session table.

    This is the code that i'm using to login within an external file.
    <?php
    require_once '/home/site/public_html/modx/config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');
    $modx->getService('error','error.modError', '', '');
    
    $user = $_REQUEST;
    
    if ($user && isset($user['username'])) {
     $loginContext = $modx->context->get('key');
     $c = array(
       'login_context' => $loginContext,
       'username' => $user['username'], 
       'password' => $user['password'],
       'returnUrl' => null,
       'rememberme' => false
     );
    
     $response = $modx->runProcessor('security/login', $c);
     
    return true;
    
    }
    


    Any ideas?
      • 3749
      • 24,544 Posts
      Try hard-coding the context key in line 11.

      $loginContext => 'web';
        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
        • 36926
        • 701 Posts
        Quote from: BobRay at Jun 22, 2016, 05:03 PM
        Try hard-coding the context key in line 11.

        $loginContext => 'web';
        Thanks Bob for the reply, unfortunately this didn't seem to make any difference.

          • 4172
          • 5,888 Posts
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 36926
            • 701 Posts
            Quote from: Bruno17 at Jun 29, 2016, 09:20 AM
            this is working for me, for logging in by curl at a frontpage with a login-snippet-call:

            https://github.com/Bruno17/migxBackupRestore/blob/master/core/components/migxbackuprestore/elements/snippets/mbrclientinstallerwidget.snippet.php#L63

            Thanks Bruno, I'm not actually see that much of a difference in what you have. Here's the what I have

            // set URL and other appropriate options
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  0);
                    curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
                    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
                    curl_setopt($ch,CURLOPT_POST, count($userData));
                    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
            
                    $distributors =  curl_exec($ch);
            
                    curl_close($ch);
            
                    return  $distributors;
            


            I see you have
            //Handle cookies for the login
            curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
            

            Is this need and if so what's inside cookie.txt?

              • 4172
              • 5,888 Posts
              since you haven't a browser, where cookies are stored to your machine, you will need a file, where to store the cookies.
              They are stored into that text-file.

              http://php.net/manual/en/function.curl-setopt.php


              CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 36926
                • 701 Posts
                So that would sort of make sense then as it wouldn't be setting the PHPSESSID cookie.
                I've tried adding CURLOPT_COOKIEJAR but that doesn't seem to of made a difference, but it doesn't look like the cookie.txt file is actually getting created.
                  • 36926
                  • 701 Posts
                  Ok so I can now see the PHPSESSID within cookie.txt file and this ID matches up the ID set in in the modx_session table although the data value looks a little strange:
                  username|N;password|N;modx.user.contextTokens|a:1:{s:3:"web";i:2;}modx.web.user.token|s:52:"modx577435003ca887.97046717_257753ad93a2157.20556569";modx.web.session.cookie.lifetime|i:0;
                  

                  So i need to some how get that out and set it in the browser.
                    • 4172
                    • 5,888 Posts
                    can't you just do your stuff, before you curl_close($ch) ?
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 36926
                      • 701 Posts
                      So what would you do before the curl_close($ch) to set the modx session.

                      I thought this was just related to curl, but am now running the login process direct in the magento script and that also return a success message but no session created.

                      Quote from: Bruno17 at Jul 01, 2016, 12:06 AM
                      can't you just do your stuff, before you curl_close($ch) ?