We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Any idea how I can easily get a referring resources id for use in a snippet?

    I need to be able to display different menus based on the [ultimate] parent id of the referring page.

    -thanks
    -sean
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 38878
      • 255 Posts
        • 3749
        • 24,544 Posts
        You can do it through $_SERVER['HTTP_REFERER'], but it's not always reliable and you'd have to extract the alias from the URL with preg_match() and do a getObject() to get the referring resource object, then get its ID.

        You might consider just putting the referring ID in a $_GET variable in the URL or writing it to a $_SESSION variable using a snippet in the template, then grab it with a snippet on the page you're displaying. I think that would make for faster page loads.

        If you use a $_SESSION variable, make sure the code that grabs the ID is above the code that writes it. wink
          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
          • 26503
          • 620 Posts
          ok - trying to store this in a session variable and retrieve it... driving me $^%#ing nuts. Something odd is going on with the FURLs & .htaccess ...

          I call [[!GetReferrer]] at the head of my templates, which executes this [just echoing stuff now for debugging]

          <?php
          // get session variable for referrer id
          $referringid = $_SESSION['referrerid'];
          
          
          // set the current referrer it
          $currentid = $modx->resource->get('id');
          
          $_SESSION['referrerid'] = $currentid;
          
          
          echo '<pre>';print_r($_SESSION);echo '</pre>';
          
          // return the old id 'false' if not found
          if($referringid){
          
          	echo 'referring id = '.$referringid;
          
          }else{
          
          	echo 0;
          
          }




          But what it is doing is returning the pageid of the 404 file most of the time, every once in a while it will return & save the correct page ids, but nothing repeatable!! [ARGH!]

          - I disabled all cache, no dice
          - disabled FURLs, again no dice
          - removing the .htaccess, DICE!

          It works fine when the .htaccess is disabled - but I don't see anything about the .htaccess that could be causing pretty much every page to be the 404 page!
          It's completely stock - the only uncommented lines are:

          RewriteEngine On
          RewriteBase /
          
          # The Friendly URLs part
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]



          What in the heck is going on here!?!??!


            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525
            • 3749
            • 24,544 Posts
            This is working for me when navigating around with a Wayfinder menu:

            // get session variable for referrer id
            $referringid = isset($_SESSION['referrerid'])? $_SESSION['referrerid']: 0 ;
            // set the current referrer it
            $currentid = $modx->resource->get('id');
             
            $_SESSION['referrerid'] = $currentid;
             
             
            echo '<pre>';print_r($_SESSION);echo '</pre>';
             
            // return the old id 'false' if not found
            if($referringid){
             
                echo 'referring id = '.$referringid;
             
            }else{
             
                echo 0;
             
            }


            The original code throws an E_NOTICE error on the first page.
              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
              • 26503
              • 620 Posts
              What browser Bob? After doing some testing it's only happening with Chrome... which is actually even more confusing.

              Still haven't got a clue what is happening.....
                *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                Sean Kimball CLP, CLS.
                Technical Director / Sr. Developer | BigBlock Studios
                ._______________________________________________.
                Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                phone/fax: 905-426-5525
                • 26503
                • 620 Posts
                Thanks for the PMs Bob.

                You're not gonna believe this one.. by tailing the server logs:

                174.92.104.115 - - [28/Jan/2013:12:32:25 -0500] "GET /oshawa/specialized-clinics HTTP/1.1" 200 1806 "http://opcs.bigblockstudios.ca/oshawa/contact-us" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17"
                174.92.104.115 - - [28/Jan/2013:12:32:25 -0500] "GET /favicon.ico HTTP/1.1" 404 1773 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17"


                notice the 404? seems as if modx is delivering the 404 page to the browser for that request! If I touch a favicon.ico file the problem goes away & I get the correct referrers.

                Not sure if this qualifies as a bug [well technically it is not] - or if we need to poke the devs about it. What do you think?
                  *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                  Sean Kimball CLP, CLS.
                  Technical Director / Sr. Developer | BigBlock Studios
                  ._______________________________________________.
                  Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                  27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                  phone/fax: 905-426-5525
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  If the web server doesn't find the resource requested, then it rewrites the request to hand it off to the MODx index.php file. If MODx doesn't find anything, then it returns the Not Found page. You can adjust your rewrite rules to not rewrite .css, .js, or .png, .jpg, or .ico files, or you could use a plugin for "OnPageNotFound" to deal with the matter.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 26503
                    • 620 Posts
                    Quote from: sottwell at Jan 28, 2013, 01:43 PM
                    If the web server doesn't find the resource requested, then it rewrites the request to hand it off to the MODx index.php file. If MODx doesn't find anything, then it returns the Not Found page. You can adjust your rewrite rules to not rewrite .css, .js, or .png, .jpg, or .ico files, or you could use a plugin for "OnPageNotFound" to deal with the matter.

                    Ah so there is an extra already - I was thinking about using the .htaccess [or even a modx favicon.ico resource!] just in case that file went missing, would screw up the navigation for all chrome users !
                      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                      Sean Kimball CLP, CLS.
                      Technical Director / Sr. Developer | BigBlock Studios
                      ._______________________________________________.
                      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                      phone/fax: 905-426-5525
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      I'm not aware of any existing extras for that.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org