Jump to content

Number of active users


EsOne

Recommended Posts

I am *still* new to PHP, and are looking to make something that shows how many people are currently using my site.

 

Only problem is. I have no idea how to.

 

I know I have to create a data base that logs ips, but after that, I am clueless.

 

Any help would be fantastic. If possible, I would like it dummied down, and the "why this does this" thing, instead of just the code.

Link to comment
https://forums.phpfreaks.com/topic/187405-number-of-active-users/
Share on other sites

I would like it dummied down

 

1) Create a db table: sessions

2) Store session data in the db table using session_set_save_handler

3) Set session.gc_probability 1, session.gc_divisor 1 (not recommended on heavy websites)

4) Perform a query counting all rows currently in the sessions table

5) Store additional data like username, last_click_url, last_click_at and display which user is on which page

 

Things you can try with this setup:

6) Someone acting stupid? Provide a remote logoff functionality (change user to group banned, remove his session data from the db)

7) Detect when logging in to an already logged in account

k im gonna try this as well.

 

I know howto strore session, username and stuff but for the last_click how would that be done?

I'm guessing when the page loads it adds the data to the table?

 

EDIT: Also how would the data be removed automatically if user doesn't log out?

Something like this:

 

~

$baseUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$sql = "UPDATE session SET last_click_at = now(), last_click_url = '$baseUrl' WHERE ..";
mysql_query($ql);

 

EDIT: Also how would the data be removed automatically if user doesn't log out?

 

That's what the session.gc_probability and session.gc_divisor are for. But pfmabismad already mentioned in another post that this is best not be done. You can however set session's to expire after a certain time and when using something like google analytics (or manually) you can track how long a user remains on your website and increase if necessary.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.