Protecting Pages by Age

Learn an easy way to control your website members’ access to content based on its publication date.

By Bob Ray
December 16, 2021
Protecting Pages by Age

These days it’s common for websites to offer early access to content to subscribers or donors. This tutorial offers an easy way to accomplish this with User Groups and a short Snippet.

This will require that you have set up User Groups and a login page for your site’s members to access their premium or early access content.

In this tutorial, a MODX user is looking to create User Groups whose members could only see pages of a certain age, that is, how long ago they were first published. The “Zero” User Group could see all pages. The “Sixty” User Group could only access pages that were more than 60 days old. The “Ninety” User Group could only see pages that were more than 90 days old.

It’s possible to do this with Resource Group Access ACL entries, a cron job to put pages in the appropriate group as they age, and a Plugin to put new pages into a separate protected Resource Group, but there’s a much simpler and more reliable way.

The answer is a simple Snippet in the page Template for all protected pages that checks the age of the page and the current User’s User Group and acts accordingly.

Let’s call the Snippet “AgeCheck”. Here is the tag for the Template:

[[!AgeCheck]]

We call the Snippet uncached (with the exclamation point) so that the results will always be up-to-date for the individual user.

Here is the code for the Snippet:

/* AgeCheck snippet */
$currentUser = $modx->user;

/* make sure user is logged in */
if ($currentUser->hasSessionContext('web')) {
    /* get current timestamp */
    $now = time();
    /* get the page's birthdate as a timestamp
       (we could use 'publishedon' or 'editedon' here) */
    $pageBirthday = strtotime($modx->resource->get('createdon'));

    /* Get page age in days */

    /* rounds up to the nearest day */
    $pageAge = ceil(($now - $pageBirthday) / 86400);

    /* Alternative: rounds *down* to the nearest day */
    // $pageAge = floor(($now - $pageBirthday) / 86400);

    /* Check the user's User Group */
    if ($currentUser->isMember('Zero')){
        return '';
    }

    if ($currentUser->isMember('Sixty') && ($pageAge >= 60)) {
        return '';
    }

    if ($currentUser->isMember('Ninety') && ($pageAge >= 90)) {
        return '';
    }
}
/* User fails all tests, cannot see the current page */

return 'Sorry, you are not authorized to see this page';

// or 

$url = 'http://full/url/of/sorry/upgrade-your-subscription/page'; 
$modx->sendRedirect($url);

The return ''; line essentially does nothing and the user gets to see the page.

The strtotime() call is necessary because $modx->resource->get() returns a human-readable date/time when used with date fields like createdon, publishedon, and editedon. We have to use strtotime() to convert it into a timestamp, which is what is returned by time(). Because a timestamp is in seconds, we need to divide the difference between now and the page’s birthday by 86400 to convert it to days (which we then round either up or down to the nearest day).

It’s relatively easy to convert the code above to work with different page ages, and with a little work, you could send members of the “Sixty” or “Ninety” User Groups to a custom page that suggests upgrading to a User Group with access to newer pages and a subscription link that upgrades them.

There is one downside to using this method rather than using ACL entries. There’s no easy way to hide pages in the menu that the user doesn’t have access to. On the other hand, showing those pages provides an incentive for users to upgrade their memberships. It should be possible with a custom output modifier in one of the menu’s Tpl Chunks to make the menu entries for those pages be plain text rather than links, but that would keep the user from trying to access them and being sent to the upgrade page.

Another way to go would be a custom output modifier that changes the style of pages in a certain age group or adds a custom icon to them in the menu.

For more on protecting pages for members only, see Create Protected Pages the Easy Way.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.