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

    I want to redirect visitors on their first visit of a specific page. It would be ok to set a cookie that expires after x hours to detect this. Currently I'm doing this in javascript, but this is - as you can imagine - pretty slow, because the first page always gets load.

    I was thinking about writing a plugin that's launched on OnWebPageInit, checking whether it is the right page, checking whether the plugin is set, if not set it and redirect, if it is set do nothing. I tried a little around but don't get it to work.

    Any ideas or pointers? Thanks a lot already!
      • 3749
      • 24,544 Posts
      I think the most reliable thing would be to use a snippet to create and check either a User Setting or an extended field in the User Profile (I think the first one would be faster).

      If you want to do this with many pages, a plugin might make more sense, but if not, you could just put the snippet tag in the content or template of the pages you want to protect.

      How many pages do you want to protect this way?


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 36932
        • 39 Posts
        Hi Bob,

        thanks for your answer.

        In the first place, I would want to do this for only one page, but it would still be nice to have this as a plugin, e.g. to control it via template variables. A snippet would be a start, for shure.

        The problem about your approach is, that my user's won't be logged in, but general website visitors.

        With kind regards,
        Marc
          • 3749
          • 24,544 Posts
          Ah ... I thought they would be logged in. You could use a $_SESSION variable, but it would be only be good for that visit.

          I'm not sure anything else is viable since the user could be visiting from a different browser, computer, tablet, or phone for different visits.

          if ($modx->resource->get('id') != 22) { // id of protected page
             return;
          }
          
          $v = 'visted' . $modx->resource->get('id');
          if (!isset($_SESSION[$v])) {
             $_SESSION[$v] = '1';
             $modx->sendRedirect($modx->makeUrl(12, "", "", "full)); // id of page to forward to
          }


          I would still use a snippet and take out the first "if". Otherwise you're going to execute that top code on every page load for every resource for no reason.


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            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
            • 36932
            • 39 Posts
            Thanks a lot, very nice and simple! I just didn't thought about using a $_SESSION variable for it, this makes it quite easy. Will be tested tomorrow.