# On login alter users login count

**URL:** <https://community.modx.com/t/on-login-alter-users-login-count/4139>\
**Category:** Random\
**Created:** [June 7, 2021, 1:44pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139 "2021-06-07T13:44:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![TaoistTotty](https://community.modx.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@TaoistTotty](https://community.modx.com/u/TaoistTotty)\
**Post date:** [June 7, 2021, 1:44pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/1 "2021-06-07T13:44:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 7, 2021, 2:44pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/2 "2021-06-07T14:44:24Z")

</div>

- 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](https://docs.modx.com/current/en/building-sites/client-proofing/security/users#using-extended-fields) (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](https://docs.modx.com/current/en/extras/login/login.tutorials/using-pre-and-post-hooks) in the call to `Login` or maybe use a plugin in MODx that runs on [OnBeforeWebLogin](https://docs.modx.com/current/en/extending-modx/plugins/system-events/onbeforeweblogin) or [OnWebLogin](https://docs.modx.com/current/en/extending-modx/plugins/system-events/onweblogin).

---

<div class="post-metadata">

**Author:** ![TaoistTotty](https://community.modx.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@TaoistTotty](https://community.modx.com/u/TaoistTotty)\
**Post date:** [June 7, 2021, 4:01pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/3 "2021-06-07T16:01:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 7, 2021, 4:15pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/4 "2021-06-07T16:15:37Z")

</div>

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](https://community.modx.com/t/timed-publishing-and-unpublishing-of-pages/4125), if it’s for the same project.

---

<div class="post-metadata">

**Author:** ![TaoistTotty](https://community.modx.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@TaoistTotty](https://community.modx.com/u/TaoistTotty)\
**Post date:** [June 7, 2021, 4:23pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/5 "2021-06-07T16:23:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 7, 2021, 4:46pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/6 "2021-06-07T16:46:26Z")

</div>

You can find some basic information in the [MODX Documentation](https://docs.modx.com/current/en/extending-modx) 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).

---

<div class="post-metadata">

**Author:** ![TaoistTotty](https://community.modx.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@TaoistTotty](https://community.modx.com/u/TaoistTotty)\
**Post date:** [June 7, 2021, 4:47pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/7 "2021-06-07T16:47:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [June 8, 2021, 4:06am UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/8 "2021-06-08T04:06:30Z")

</div>

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:

```auto
/* 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):

```auto
$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.

---

<div class="post-metadata">

**Author:** ![TaoistTotty](https://community.modx.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@TaoistTotty](https://community.modx.com/u/TaoistTotty)\
**Post date:** [June 8, 2021, 4:30pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/9 "2021-06-08T16:30:42Z")

</div>

> [@bobray](#):
>
> ```auto
> $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);
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![bruno17](https://community.modx.com/user_avatar/community.modx.com/bruno17/32/71_2.png) [@bruno17](https://community.modx.com/u/bruno17)\
**Post date:** [June 8, 2021, 4:58pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/10 "2021-06-08T16:58:01Z")

</div>

I think, you are missing

`$profile->save();`

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [June 8, 2021, 5:08pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/11 "2021-06-08T17:08:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TaoistTotty](https://community.modx.com/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@TaoistTotty](https://community.modx.com/u/TaoistTotty)\
**Post date:** [June 8, 2021, 7:56pm UTC](https://community.modx.com/t/on-login-alter-users-login-count/4139/12 "2021-06-08T19:56:56Z")

</div>

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

Also thanks @halftrainedharry for your help.
