Ideas on how to implement notifications using $_SESSION

I am trying to build a notification system using PHP session variables ($_SESSION).

My concept is the following:

1 - Loop in a structure field to count the fields within
2 - Store resulting number in a session variable [‘alllogs’]
3 - Set [‘newlogs’] equal to [‘alllogs’]
4 - Display [‘newlogs’] as 'n notifications’
5 - Vewing the notifications will set [‘readlogs’] equal to [‘alllogs’]
6 - After event, unset all variables except [‘readlogs’]
7 - If new content is added [‘newlogs’] equals [‘alllogs’] - [‘readlogs’]
8 - Display [‘newlogs’] values as ‘n notifications’

Everything is working fine both locally and remote. BUT:

  • When the page is visited (session started) in another computer, I got notifications again for the session started in MY computer.

I know it has something to be with the logic I’m using. Initially I thought that PHP were not using cookies for sessions, but it is.

What I need is either Ideas on how to fix this, or even better, how to tie the notifications to the user instead of session. Somehow add the values in a field on the page and compare it? It is possible to write/modify a field value directly from the page?

Any feedback is appreciated.

Local storage (save data with JS in the browser), cookie or store an IP number is the first that comes to mind.

A php session is always connected to a device, not a user, because php does not know about (kirby) users.
Another thing is, that I don´t know how long sessions are stored (unless you destroy it in your code). So values stored in a session should not be used as they were store forever !?

For adding custom values to the user object you should take a look at the user class kirby/core/user.php. You probably can add data to the user object with the $user->update method and retrieve it with the $user->data method.
I have not tested this, but it seems to be the right place. Take a look at the user´s file in /site/accounts to check whether the data is stored.

I like that idea of storing data to the users file, I am doing something similar in a current project and it works really well. I am storing current states of the page according to the user. It works well with ->update(); you can define what ever key/value you like.