We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38357
    • 178 Posts
    Hello,
    Using Revolution 2.2.4-pl with menu generated by Wayfinder.

    I need to log on a user with Login on my ModX site and then provide a menu link to an external website which automatically logs the user in to the remote site and returns the appropriate web page in a new window.

    I can provide a suitable menu link using a WebLink resource, but how do I Post the data to log the user on to the remote site?

    When a user logs on using Login I have a php function in a snippet which gets the username and other needed info, encodes it and stores it in a string.

    In order for the user to access the page on the remote website I need to Post this encoded string containing the username and other info to the other webserver in the URL. The remote server then decodes the string, logs the user on and returns the requested web page.

    Any suggestions as to how this can be achieved using the WebLink?
      • 3749
      • 24,544 Posts
      You could have your snippet create and return an HTML form where all the fields are hidden except for the submit button (which would be the link) and the form's action was the remote URL. The only down side is that the login info would be visible via "view source."

      Offhand, the only way I can think of to avoid that is to use cURL.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 38357
        • 178 Posts
        Thanks for the response Bob.
        I've been researching for a while and it does look as if cURL might be the best option. I thought that perhaps someone had done this before so I didn't have to 'reinvent the wheel' using my limited skills smiley
        I guess I could call the snippet containing the Curl script from the WebLink?
        I often wonder why so many say 'but you might have to use cURL' as if it has some strange disease ?
          • 3749
          • 24,544 Posts
          It's not usually my first choice. wink

          Debugging it is like debugging email -- you know it doesn't work, but you often get insufficient information on why it's not working. And it's quite slow.

          Trivial fact: There are about 5,000 instances of the string 'curl' in my MODX core directory (though some are in extras I've installed or written).


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            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
            • 38357
            • 178 Posts
            Thanks again Bob,
            I have created a Weblink page and inserted a chunk call in the content -
             [[!$MyChunk]]
            The link appears in the Wayfinder menu as intended.
            The chunk works and redirects to the required website when it contains the URL - http://wwww.website.com

            I'm not sure how to post the required information to the URL. At the moment I have created a snippet as below:
            <?php
            
            //function to create and encrypt string to be POSTed
            
            
            function Q_encrypt($string) {
            return base64_encode($string);
            }
            
            $key="71835e0a-1d05-4a84-9477-598ec1f2e2a2";
            $date=strftime('%d%m%Y');
            $mylink="31016";
            $string=$key."_".$mylink.$modx->user->get('username')."_".$date;
            $ecr=Q_encrypt($string);
            
            
            
            
            # HTTP Post using cURL
            
            
            function do_post_request($fields, $optional_headers = null ){
            // http_build_query is preferred but doesn't seem to work!
            // $fields_string = http_build_query($fields, '', '&', PHP_QUERY_RFC3986);
            
            // Create URL parameter string
            foreach( $fields as $key => $value )
            $fields_string .= $key.'='.$value.'&';
            $fields_string = rtrim( $fields_string, '&' );
            
            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_POST, count( $fields ) );
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_string );
            
            $result = curl_exec( $ch );
            
            curl_close( $ch );
            }
            
            do_post_request($ecr);
            ?>
            


            and simply appended this to the URL in the Chunk - http://wwww.website.com MySnippetCallHere

            I'm sure this is incorrect, but just cannot find how to code it correctly. What technique should I use here? [ed. note: bobd72 last edited this post 14 years ago.]
              • 3749
              • 24,544 Posts
              Since you have all the information, could you just forward the user with $modx->sendRedirect($url) instead of displaying the link?


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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
                • 38357
                • 178 Posts
                I don't think so - because it needs to be optional.
                The website is a 'love job' for my local club. Members need to be able to log on to the site to get results etc and have the option to make bookings for times of use on a website provided by someone else, so I need a menu link so that they can get the info they need from the club site, then connect to the other site to reserve their times.

                I am trying to avoid having two different usernames and passwords(most members are over 70) . Having them log on to the club site then be able to go directly to the booking site without logging on there as well is essential - to much complication confuses many (it's hard enough to get them to use the internet smiley )

                How would I use $modx->sendRedirect($url) to POST info to another site so that they can decode and use it to log the user automatically onto their site? They can receive and decode the data if I POSTed in the link.

                I have found the format of the command in your book but don't know how to apply it in this case?

                Thanks for your assistance - it is greatly appreciated.

                [ed. note: bobd72 last edited this post 14 years ago.]
                  • 3749
                  • 24,544 Posts
                  AFAIK, the only way to do it with sendRedirect() would be to put the data in a query string at the end of the URL and have the other end use $_GET, which you probably don't want to do.


                  BTW, the foreach in your code could use some curly braces. wink


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    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
                    • 38357
                    • 178 Posts
                    Reply
                    AFAIK, the only way to do it with sendRedirect() would be to put the data in a query string at the end of the URL and have the other end use $_GET, which you probably don't want to do.
                    I could probably do this by just appending it to the URL in the chunk and referencing the chunk from the WebLink page. But of course that means it is visible in the URL

                    BTW, the foreach in your code could use some curly braces.
                    Hmm - yes, so it should :-( Thanks.

                    It would seem that if using a Weblink to ensure that the page appears in a Wayfinder generated menu it is not possible to POST data. Very frustrating. Calling a Snippet which contains sendRedirect() from a document redirects to the other website OK but does not open in a new tab/window as it does with a Weblink page.

                    How does Modx open a new page in a new window - there must be a method Weblink uses?
                    [ed. note: bobd72 last edited this post 14 years ago.]
                      • 3749
                      • 24,544 Posts
                      Try this:

                      $modx->sendRedirect($url . ' target="_blank" ';


                      (Note the space between the single quote and the word target. )



                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html
                        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