How to get the count of sessions?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aristona
Forum Commoner
Posts: 33
Joined: Thu Dec 02, 2010 8:14 am

How to get the count of sessions?

Post by Aristona »

Hi,

I have a page where users should login to browse the content. When they login, I create a session for them.

My question is;

1. How can I see the total count of current sessions?
2. How can I see the each username of sessions? (like Aristona in this case)

Thanks.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: How to get the count of sessions?

Post by curlybracket »

You have to use database, I'm not sure if it is possible to count sessions like that.
I think you have to create table for this purpose and put users data inisde. Gather data in the same place in your code where sessions are being created.
Aristona
Forum Commoner
Posts: 33
Joined: Thu Dec 02, 2010 8:14 am

Re: How to get the count of sessions?

Post by Aristona »

curlybracket wrote:You have to use database, I'm not sure if it is possible to count sessions like that.
I think you have to create table for this purpose and put users data inisde. Gather data in the same place in your code where sessions are being created.
Thanks for the reply, but what about when someone closes the browser but doesn't click the logout button? Do I need to define idle duration and kick users automatically? If so, any examples on how can I do it?
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: How to get the count of sessions?

Post by curlybracket »

You can store time of logging in this new table. Than you can refresh it after user's actions and check if last refresh is not older than session time. If it is - he probably closed his browser without logging out.
Aristona
Forum Commoner
Posts: 33
Joined: Thu Dec 02, 2010 8:14 am

Re: How to get the count of sessions?

Post by Aristona »

curlybracket wrote:You can store time of logging in this new table. Than you can refresh it after user's actions and check if last refresh is not older than session time. If it is - he probably closed his browser without logging out.
A db connection -> updating rows each time he visit a page won't be a problem with website's performance?
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: How to get the count of sessions?

Post by curlybracket »

It depends on the site. Counting logged users will usually be just an approximation. So you can update row only when user do some specific things, ie. updating database in some way or visiting home page. Or you can just assume that average visit time == 20 minutes and treat user as logged off after that time.
Post Reply