We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24374
    • 322 Posts
    This is driving me crazy. I have a session variable to keep track of which header photo is being displayed, so that I can rotate them on each page view. Here's the code:

    if(!isset($_SESSION['headerphotonum'])) { $_SESSION['headerphotonum']=-1; }
    $_SESSION['headerphotonum']=$_SESSION['headerphotonum']+1;
    echo '-'.$_SESSION['headerphotonum'].'-';
    


    This is supposed to add 1 to the variable each time a page loads. (The third line is in there just so I can see what the number is each time I refresh a page.) But what happens is that the number gets increased by anywhere from 4 to 6 instead of 1, like the script is firing 4 to 6 times with each page load. I tried putting this code in different scripts in different parts of the template, all with the same result. This session variable is referenced nowhere else in any other scripts.

    I have the main script "headerPhoto" in the template like this:

    . . .
    <div id="header">[[header1]][[!headerPhoto]][[header2]]</div>
    . . .
    


    What on earth is going on? Do uncached scripts really load more than once with each page view???
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Hmm... trying it I get a 0, then it adds 2 with each page load. I did change the snippet code to output the results instead of echo it, but it's still increasing by 2.
      <?php
      if(!isset($_SESSION['headerphotonum'])) { $_SESSION['headerphotonum']=-1; }
      return $_SESSION['headerphotonum']++;
      [ed. note: sottwell last edited this post 12 years, 6 months ago.]
        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
        • 3749
        • 24,544 Posts
        Some Google tools or the like will make a request to the same page after each legitimate request.
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I'm just using a fairly new installation on Localhost with nothing but TinyMCE, pdoTools and Ace installed.
            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
            • 24374
            • 322 Posts
            There are no Google scripts installed at all. You can look at this page:

            http://dgwebdevelopment.com/

            There's nothing much there yet; I'm still developing the template. But you can watch the count in the upper left of the screen. I just refreshed and it went up by 10!! I've used this on other php sites (not MODX) and it always worked fine.

            If this isn't going to work, is there some other way to generate a number that will only increase by 1 for each page load?
            [ed. note: rainbowtiger last edited this post 12 years, 6 months ago.]
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Ok... something very fishy is going on. On my MODX Cloud installation, it works just fine. So why is it loading twice on my localhost MAMP installation?
                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
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                On further investigation, if the page is unpublished it increments by one as expected, when viewed by a logged-in Manager user. On a published page, not logged in at all, it again increments by one as expected. So MODX is doing something odd with the SESSION when logged in.

                Well, that still doesn't explain why it isn't incrementing by 2 on the Cloud, logged in or not, published or not...

                Put it in the template on the Cloud instead of in a resource, and now it's incrementing by 2. This is really weird. [ed. note: sottwell last edited this post 12 years, 6 months ago.]
                  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
                  • 24374
                  • 322 Posts
                  And it's not making any difference for me if I'm logged in or not. I also tried converting the script from static to non-static, and that didn't do anything. The other weird thing is that it's not regular; I've had the number increase by anywhere from 4 to 15 in no particular sequence.


                  _______________________________

                  OK, I just turned off "session_enabled" and put session_start() in my header. No luck. On first refresh it went up by 4. On second refresh it went up by 25!! [ed. note: rainbowtiger last edited this post 12 years, 6 months ago.]
                    • 24374
                    • 322 Posts
                    The only thing I can think of to do at this point is to put a time check in there: only increment the number if more than 2 seconds have passed.
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Is it possible that the SESSION is being hit with every request for images, js scripts, etc?
                        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