We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52257
    • 11 Posts
    Hi,

    I would like to get a random int from a snippet. However, it get cached. any idea?

    In the chunk -
    [[!rand]]

    in the snippet rand -

    <?php

    echo rand();

    ?>

    the number will get refresh after flushing cache.

    Please help me.

    thanks

    Keith
      • 3749
      • 24,544 Posts
      I tried your code and I get a different number each time. The only thing I can think of is that you have a very old version of PHP. There's a version with a bug in the rand() function.

      It that's the issue, you could try this:

      function make_seed()
      {
        list($usec, $sec) = explode(' ', microtime());
        return (float) $sec + ((float) $usec * 100000);
      }
      srand(make_seed());
      return rand();


      Another option is to use uniqid() instead of rand(). It uses microtime() for the seed and should give you a unique string each time. It's a better option if you want a string that's truly unique (say, for a filename), though the string will include letters as well as numbers.

      If you don't want the letters, you can do this:

      return hexdec(uniqid());
      [ed. note: BobRay last edited this post 10 years, 4 months ago.]
        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
        • 52257
        • 11 Posts
        Quote from: BobRay at May 14, 2016, 11:35 PM
        I tried your code and I get a different number each time. The only thing I can think of is that you have a very old version of PHP. There's a version with a bug in the rand() function.

        It that's the issue, you could try this:

        function make_seed()
        {
          list($usec, $sec) = explode(' ', microtime());
          return (float) $sec + ((float) $usec * 100000);
        }
        srand(make_seed());
        return rand();


        Another option is to use uniqid() instead of rand(). It uses microtime() for the seed and should give you a unique number each time. It's a better option if you want a number that's truly unique (say, for a filename).

        Hi Bob,

        I tried your code, still no luck.

        my php version is PHP 5.3.29

        server - AWS instance

        modx version - MODX Revolution 2.2.14-pl (traditional)

        Any idea?

        thanks

        Keith
          • 3749
          • 24,544 Posts
          I wonder if AWS is caching the page?

          You could try making the resource itself uncached by unchecking the cacheable checkbox to see what that does.
            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
            • 52257
            • 11 Posts
            Quote from: BobRay at May 14, 2016, 11:46 PM
            I wonder if AWS is caching the page?

            You could try making the resource itself uncached by unchecking the cacheable checkbox to see what that does.

            Hi,

            I tried it as well.

            Here is my url

            https://international.jets.com.au/

            echo uniqid();
            return include $modx->getOption('base_path').$file;

            <?php
            echo file_get_contents('https://international.jets.com.au/customSnippets/homePageBanner.php');
            ?>

            testing url - https://international.jets.com.au/customSnippets/homePageBanner.php - not cached if I call externally [ed. note: keith_doctrinaire last edited this post 10 years, 4 months ago.]
              • 52257
              • 11 Posts
              still getting from cache
                • 52257
                • 11 Posts
                ok, I disabled xFPC, and it works.. thanks Bob anyway!
                  • 3749
                  • 24,544 Posts
                  I'm glad you got it sorted. BTW, you should really upgrade MODX.

                  A number of important security vulnerabilities have been fixed since that version. I recommend using the UpgradeMODX Widget for the upgrades and I would upgrading in steps from your current version to to 2.3.0, 2.4.0, and then 2.4.4.
                    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
                    • 52257
                    • 11 Posts
                    Quote from: BobRay at May 15, 2016, 05:45 PM
                    I'm glad you got it sorted. BTW, you should really upgrade MODX.

                    A number of important security vulnerabilities have been fixed since that version. I recommend using the UpgradeMODX Widget for the upgrades and I would upgrading in steps from your current version to to 2.3.0, 2.4.0, and then 2.4.4.

                    thanks for your suggestion, we will do it soon.

                    :)