We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42560
    • 49 Posts
    Hi, i have a snippet with code to generate a popup in any given page. this code

    $ran = array(25315,23057,25316,7279,4599,23574);
    $randomElement = $ran[array_rand($ran, 1)];

    this randomElement is what im gonnna use to do the logic.

    is not working properly. $randomElement is a random number from the array of 6 elements. the thing is that i can see that the page is generating the popup correctly when im logged in to modx, but if not(example, i opne the same page in Firefox, no logged in to modx) the page is not generating the random code, just the same every time i refresh the same page.

    when i enter another different url, it generates well until i refresh the same page.

    snippet is uncached !randomsnippet.
    and same with the chunk


    [[!$mainchunk]]
    [[!randomsnippet]]

    thanks in advance
      • 3749
      • 24,544 Posts
      That sounds like the browser cache. Is there a way you can cache-bust by adding a query string to the end of the URL like this?

      your/url/? &cb="some_random_number"


      If not, you could do something like this: https://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers
        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
        • 42560
        • 49 Posts
        Quote from: BobRay at Sep 12, 2017, 04:58 AM
        That sounds like the browser cache. Is there a way you can cache-bust by adding a query string to the end of the URL like this?

        your/url/? &cb="some_random_number"


        If not, you could do something like this: https://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers

        Thanks for replying, but unfortunatley, php headers are not working.

        i tried random params in my snipet or chunk(using &param=`rand()` ) too, used rand() in my url inside a href. none of this works sad

        always the same ID(everytime i refresh) the page. as i said before, it works perfectly if im logged in to modx.

        rand() works ok

        This is the site im working on(spanish XD)

        http://www.saludymedicinas.com.mx/centros-de-salud/acne/ (try to refresh a couple of times, a popup will appears in the bottom)
          • 3749
          • 24,544 Posts
          Adding ?cb=## where ## is a number you change, or the current timestamp works for me, though TBH I'm not sure how to accomplish that in code in your situation.

          One way might be to use a snippet in side your URL:

          <a href="your_url?cb=[[time]]">

          /* time snippet */
          return time();


          I think it's possible to alter the URL with a plugin, but I can't remember how to do it.

          Another way to go, since you're creating a popup, would be to do the random selection in your JavaScript.
            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
            • 42560
            • 49 Posts
            mhm, nope, i attached a random number to my url, to my snippet/chunk as a param, but it doesnt work.

            it works if im logged in to the manager and go to the site to check that everything is ok. if i log out, the random number is there, but even if ai refresh the same page a million times, the popup is not changing unles i go to another page... thanks for the help