On login alter users login count

I am using the login Extra, and I wondered if anyone new how to automatically decrease a number on the ‘users’ profile, and then when this reaches 0 to stop entry and redirect to another page?

This is for a members page and deals with counting down the amount of entries members have paid for

  • When should this number be decreased? When the user logs in or when the user accesses the member page?
  • Should the user still be able to log in when the number is 0?

To save the value you could use the extended field (or maybe you could just use another field of modUserProfile for this purpose).


To check/decrease the value on login you can either use a pre- or posthook in the call to Login or maybe use a plugin in MODx that runs on OnBeforeWebLogin or OnWebLogin.

Thanks @halftrainedharry Ideally it should be decreased when the member accesses a specific page

Yes the member should be able to log in so they can access a payment page and have any increase added to their profile.

Is there something that can be run on the pages?

You could run an (uncached) snippet on the page or use a plugin.

Maybe you could combine it with the solution of your last question, if it’s for the same project.

Thanks. Is there anywhere I can learn how to do this kind of thing?

You can find some basic information in the MODX Documentation or try to find some code you can adjust to your purposes in the forum. Some basic knowledge in programming PHP is also useful.


Depending on what your users do on the member page, it may be better to link the decrease of the value to an action (for example a form submit).

Thanks very much for this.
Time to dust off some very old php knowledge

I’d suggest using the fax field in the user profile (assuming that you don’t need it for fax numbers). You can edit it manually in the Manager. but AFAIK, the user can’t change it.

The snippet would go at the top of the page:

[[!LimitAccess]]

The code might look something like this:

/* LimitAccess snippet */ 
$profile = $modx->user->getOne('Profile');
if ($profile === null) {
    $modx->log(modX::LOG_LEVEL_ERROR, 
        '[LimitAccess snippet] User ' .
        $modx->user->get('username') . ' has no user profile.');
    return 'No Profile';
}
$count = $profile->get('fax');
if ($count <= 0) {
    $modx->sendRedirect('full/url/of/buy-more-access/page');
}

$count--;
$profile->set('fax', $count);
$profile->save();

return '<p class="count_message">' . ' You can access this page ' . 
    $count . ' more times.</p>';

This assumes that the user would have no reason to reload the page (e.g., submitting a form).

The ‘Buy-more-access’ page could use something like this for adding access (or you could update it manually):

$profile = $modx->user->getOne('Profile');
if ($profile === null) {
    $modx->log(modX::LOG_LEVEL_ERROR, 
        '[LimitAccess snippet] User ' .
        $modx->user->get('username') . ' has no user profile.');
    return 'No Profile';
}
$count = $profile->get('fax');
$count += $addedAccessTimes;
$profile->set('fax', $count);
$profile->save();

The “No Profile” code is probably unnecessary, but it avoids a PHP error in the case where a user is somehow created in code with no user profile and it would take just a few milliseconds to execute.

Thanks @bobray I am trying this part of the code, and nothing is changing. Is there something I am missing?

I can set the $profile->set(‘fax’, 100); and still nothing happens.
Do I need to save changes or something else?

I think, you are missing

$profile->save();

1 Like

Thanks Bruno! Yes that needs to be added to both snippets (fixed above).

Thanks @bobray and @bruno17, That solved the issue and all is working now.

Also thanks @halftrainedharry for your help.