We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42393
    • 143 Posts
    I created my first snippet as described in another thread. I found that it was executing twice, but the problem wasn't anything I had done.

    I have a home page resource based on a template. The template has a tag to call my snippet. The snippet was always executing twice. I used the Debug snippet by Oleg Pryadko to see if it would reveal any issues. It did report the following:

    (ERROR @ /index.php)[OnWebPagePrerender]
    Notice: Undefined index: browser in /home/path/core/cache/includes/elements/modplugin/6.include.cache.php on line 201
    Notice: Undefined index: browser in /home/path/core/cache/includes/elements/modplugin/6.include.cache.php on line 131
    Notice: Undefined index: browser in /home/path/core/cache/includes/elements/modplugin/6.include.cache.php on line 131

    I opened the cache file and saw it's the Mobile Detection plugin by Zuriel Andrusyshyn.

    Disabling that plugin causes my snippet to only execute once, as it should. My problem is solved.

    For the purposes of this thread, the point is that when that error is caught, my snippet only runs once. If error reporting is off (as set in the Debug snippet), the my snippet runs twice! I think this is a bug that could be occurring in a lot of sites but admins just aren't aware of it if they aren't checking the log. The issue isn't with the faulty plugin, but that an unreported exception seems to cause an unrelated snippet to execute twice.

    As to the Mobile Detection plugin, and I'll post this here because it may be related, I dunno... Line 201 doesn't reference 'browser', but line 131 does. (I'm wondering if some other script is in cache and then replaced?)

    This plugin adds a cookie called 'browser' and then attempts to retrieve it at line 131 with "return $_COOKIE['browser'];". The cookie is supposed to be set the first time the plugin is executed, as a marker to identify the browser as mobile or desktop. If the cookie is already there the plugin doesn't do anything more, but if it's not there it sets the cookie. All OK except that it's throwing an error which is explained here. I'll try to send a note to Zuriel Andrusyshyn. (See EDIT below)

    In conclusion, there is a plugin error which seems to have exposed a Revo error. If someone here thinks this is all accurate then I'll report it in the tracker - another first-time event for me here.

    Thanks!

    (EDIT)
    Apparently the cookie error in the plugin had already been reported, but my focus here is the snippet double-execution problem which is not related to any specific plugin or snippet. [ed. note: captainstarbuck last edited this post 11 years, 4 months ago.]
      Loved ModX when I was using it a few years ago. Shifted to WordPress, sorry. Thanks, all.
    • If the snippet is executing twice only when the plugin is enabled, then the issue is most likely with the plugin.
        • 42393
        • 143 Posts
        Quote from: opengeek at Dec 28, 2012, 10:34 PM
        If the snippet is executing twice only when the plugin is enabled, then the issue is most likely with the plugin.

        Thanks Jason!

        How is it possible for an error in the Mobile Detection plugin to cause a completely unrelated snippet to execute twice? That plugin isn't calling my my snippet or executing anything related. It seems more likely that the unhandled PHP exception was passed back to the caller (whatever manages the sequence of execution) which then erroneously re-executed the template and snippet.

        Is there any circumstance when you'd want an exception or any other code to cause OnWebPagePrerender to execute twice?

        And please forgive, I defer to your expertise here. I know nothing about ModX internals and I'm actually not a "competent" PHP developer either. But I do have 30 years of coding experience with almost as many languages and frameworks, and that usually allows me to deduce like this without actually seeing the internals.

        I'm happy to be wrong, but let's consider it all the way through...
        Thanks!
          Loved ModX when I was using it a few years ago. Shifted to WordPress, sorry. Thanks, all.
          • 3749
          • 24,544 Posts
          I'm not familiar with the Mobile Detection plugin, but if it redirects the user to another page or reloads the same page with different CSS, it figures that a snippet in the template would execute twice. If the plugin throws a warning, it will crash with error reporting on and fail to reload or forward, so the snippet would only execute once.
            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
            • 42393
            • 143 Posts
            Fair enough. Thanks.

            This isn't critical now, I'm just thinking about database queries that we may not want to repeat. If we do not want our code to execute twice in a single request, I'm thinking PHP code might want to leave behind some state data just for the current request. Would we use a static variable? $GLOBALS[]? For the current little snippet I have, and most I suspect, I'd just let it execute again.

            Thanks.
              Loved ModX when I was using it a few years ago. Shifted to WordPress, sorry. Thanks, all.
              • 3749
              • 24,544 Posts
              If the Mobile Detection plugin reloads the page, you don't have much choice unless you don't want the snippet to execute on the second load (which is likely the one the visitor will see).
                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
              • There is the $_SERVER['HTTP_REFERER'] value, but that's not particularly reliable. The only other way to pass data from page to page is via the URL or a cookie, which is how PHP controls SESSIONs.

                Here is an interesting series of articles culminating in using HTML5's local storage with PHP http://www.devshed.com/c/a/PHP/The-PHP-Plugin-Pattern/
                  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
                  • 42393
                  • 143 Posts
                  Sue, you're talking about client state. This is all on the server. If state is stored in the client, it's between postbacks. I'm talking about a place where we can maintain state during a single postback. For example, the $modx variable is global and carried through the application during a request, not a session (as I understand it). So maybe there is an assigned place for plugins to carry state there? Something like $modx->requestState['pluginname'] ? It wouldn't be appropriate for plugins to pollute that global with undefined data, but if that becomes an accepted convention then... ? That would also allow plugins to communicate, identify conflicting or redundant functionality, etc. (Way off topic sorry)

                  I just looked at the Mobile Detection code. It doesn't redirect. In the OnWebPagePrerender event it goes through a series of filters to whittle down $modx->resource->_output. That's all it does. In short the developer puts tags in their code, <standard> and <mobile>, and the code which does Not apply to the current browser gets removed from the page. It's very similar to my noobish isOldBrowser snippet which just uses the If/Then/Else to conditionally render specific blocks, except Mobile Detection removes unwanted blocks from the page.

                  No, I'm coming back to the original problem:
                  If this specific unhandled exception (Notice: Undefined index) is fired by a plugin, somehow the life cycle is getting processed twice. If the Debug snippet catches the exception, the page is only processed once. I don't know enough to generalize further as to whether its unhandled exceptions in general, or if it just happens at this specific point in the life cycle, or if it's specific life cycle events that get reprocessed, etc. I also can't tell you which specific debug feature changes the behavior. I'm just pointing out a specific reproducible anomaly that may cause other anomalous behavior - and hoping someone with insight in this area finds it worthy of an issue report.

                  HTH
                    Loved ModX when I was using it a few years ago. Shifted to WordPress, sorry. Thanks, all.
                    • 3749
                    • 24,544 Posts
                    $_SESSION variables will persist across multiple requests as long as you don't mess with the MODX session.

                    Thanks for digging into the Mobile Detection plugin. Your issue is more interesting now, but I can't think of anything that would cause it unless there is another plugin involved. You could selectively disable other plugins (if there are any) and see if that has any effect.
                      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
                    • Ah, I was thinking of a way to indicate that the snippet was already run on a second page load, presuming that there is a redirect, forward, or some other kind of page reload involved.

                      If it's something that's going to be used within a single page generation, then there are lots of ways to deal with it. using the $_GLOBAL array would work, as would putting it in a placeholder. You could even make a custom system setting, and get and set it as needed. I'm sure that there are other ways; I'm still not very well versed in the inner workings of Revo.
                        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