Jump to content

"there Are 14 Users, 38 Guests..."


omgRawr
 Share

Recommended Posts

How can you monitor with PHP how many users and guests are on a single page? I know that you could query information from the database to determine how many users are online, for example:

select username, current_page from users where online = 1 and page = 'news'

But, how can you monitor how many guests are online? Do I have to use the same method for guests - add a row to a table containing the guests currently logged on? Would something like this work?

create table all_users (  id int not null auto_increment,   last_activity datetime not null,   ip_address varchar(32) not null,   user_agent varchar(255) not null,   page varchar(64) not null,   referer varchar(255) null,   primary key (id)

Then if I wanted to check how many guests are viewing a page:

select count(distinct ip_address) as views from all_users where page = 'news'

And that should work, but there's another problem. How can I delete the row from the database when users leave my website? If there are only guests and there is no logout feature, then how can I delete their "session" (not really a session, just their row in the database). How can I delete their "session" if they just exit out of their browser window instead of pressing a button?Any help would be appreciated, thanks.

Link to comment
Share on other sites

Define a timeout length, say 15 minutes (or whatever is appropriate to your content). Every time you update the users/guests database, examine every record. (You're probably doing it anyway to generate the information.) For each record, get the absolute difference between last_activity and the current time. If that difference is greater than the timeout length, remove the record.

Link to comment
Share on other sites

Define a timeout length, say 15 minutes (or whatever is appropriate to your content). Every time you update the users/guests database, examine every record. (You're probably doing it anyway to generate the information.) For each record, get the absolute difference between last_activity and the current time. If that difference is greater than the timeout length, remove the record.
Perfect! Thank you!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...