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

    is there a possibility to limit a user’s pageviews with MODX?
    What I want to do is that a user can view 5 pages of my website and then gets a message with further instructions. He then won’t be able to view any further pages on my setup.
    Think it must be done with a cookie or/and with IP-storage. Are there any hints how to do that with MODX Revolution?

    thanks a lot for any suggestions,
    -Anselm
      MODX Free Template Base: MODX-Boilerplate | my blog (lots of MODX stuff) | my gitHub (translations) | MODX User Groups Germany (Facebook)
      • 3749
      • 24,544 Posts
      If there is an unused field in the user profile, you could put a snippet in your template that checks that and redirects the user to the "subscribe" page (with $modx->sendRedirect() ) if at the limit and updates the value if not.

      If there’s no unused field, you could do the same thing, but with an "extended" field in the user profile.

      If you wanted to use the fax field, for example, the snippet would look something like this (untested):
      <?php
      $limit = 5;
      $idOfLoginPage = 12; //set this to the ID of the Login page 
      $idOfSubscribePage = 23 // set this to the ID of the Subscribe page
      
      /* redirect users who are not logged in */
      if (!$modx->user->hasSessionContext('web')) {
         $modx->sendRedirect($modx->makeUrl($idOfLoginPage));
      }
      
      $profile = $modx->user->getOne('Profile');
      
      /* get the user's page views */
      $count = (int) $profile->get('fax');
      
      
      if ($count >= $limit) {
          /* send user to subscribe page */
         $modx->sendRedirect($modx->makeUrl($idOfSubscribePage));
      } else {
          $count++;
          $profile->set('fax', $count);
      }
      
      /* update the page view value */
      $profile->save();
      
      return '';


      Note that if you let the users update their profile, they’d be able to reset the page view count, though it would be inconvenient to do it every 5 page views.
        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
        • 21838
        • 284 Posts
        Thing is I want to limit all visitors not just registered users. Can this be achieved with your snippet, too?
          MODX Free Template Base: MODX-Boilerplate | my blog (lots of MODX stuff) | my gitHub (translations) | MODX User Groups Germany (Facebook)
          • 3749
          • 24,544 Posts
          Quote from: novolo at May 12, 2011, 01:31 AM

          Thing is I want to limit all visitors not just registered users. Can this be achieved with your snippet, too?

          I don’t think so. You can set a cookie, but the user can always clear the cookie or log in from a different browser or a different computer. I think the only foolproof solution is to make all users register and log in.

          The most common setup for subscription sites is to have some pages that anyone can see, but hide the rest from user’s who are not registered and logged in.
            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
            • 21838
            • 284 Posts
            Quote from: BobRay at May 12, 2011, 01:46 AM

            Quote from: novolo at May 12, 2011, 01:31 AM

            Thing is I want to limit all visitors not just registered users. Can this be achieved with your snippet, too?
            I don’t think so. You can set a cookie, but the user can always clear the cookie or log in from a different browser or a different computer. I think the only foolproof solution is to make all users register and log in.
            The most common setup for subscription sites is to have some pages that anyone can see, but hide the rest from user’s who are not registered and logged in.
            This is totally true. Client wants a solution which is most strict as possible but which also don’t has extra steps for the user. If a technically experienced guy clears his cookie and resets its IP it will be about 1 of 1000 visitors I think and that’s okay. A registration process is not intended as it decreases pageviews drastically.
              MODX Free Template Base: MODX-Boilerplate | my blog (lots of MODX stuff) | my gitHub (translations) | MODX User Groups Germany (Facebook)
              • 3749
              • 24,544 Posts
              I just realized that that’s how it’s done (cookies) on a number of real estate sites that ask you to register after viewing a certain number of properties. I never thought of clearing the cookie. tongue

              I think that method will probably work fine. wink

              Remember that cookies need to be set before any page output is sent to the browser.

              There’s an extra called CookieMonster that might give you some ideas (though it’s for Evolution).
                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
                • 21838
                • 284 Posts
                Quote from: BobRay at May 13, 2011, 01:57 AM

                I just realized that that’s how it’s done (cookies) on a number of real estate sites that ask you to register after viewing a certain number of properties. I never thought of clearing the cookie. tongue

                I think that method will probably work fine. wink

                Remember that cookies need to be set before any page output is sent to the browser.

                There’s an extra called CookieMonster that might give you some ideas (though it’s for Evolution).

                Thank you so much for all your input. That’s the reason why I think IP+Cookie is fine wink Only a very few users would check cookies and reset router to view just 5 more pages wink
                I’ve now outsourced this to Bruno17 who develops as a snippet with Cookies and custom DB for IP-check.

                thanks and best,
                -Anselm
                  MODX Free Template Base: MODX-Boilerplate | my blog (lots of MODX stuff) | my gitHub (translations) | MODX User Groups Germany (Facebook)