strobic Posted December 28, 2010 Share Posted December 28, 2010 Hi, I'm trying to code a page which displays the amount of users currently viewing a page. But how do I decrease the count if a user closes the page? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/222795-current-amount-of-users-online/ Share on other sites More sharing options...
arbitter Posted December 28, 2010 Share Posted December 28, 2010 Perhaps if you give us the code you use when the amount increases, because now we haven't got a clue so it's quite hard to help. Quote Link to comment https://forums.phpfreaks.com/topic/222795-current-amount-of-users-online/#findComment-1152066 Share on other sites More sharing options...
revraz Posted December 28, 2010 Share Posted December 28, 2010 You can't really. But you can log the time they view the page and display say the last 3 minutes. Quote Link to comment https://forums.phpfreaks.com/topic/222795-current-amount-of-users-online/#findComment-1152071 Share on other sites More sharing options...
mofm Posted December 28, 2010 Share Posted December 28, 2010 Yhe i agree with above your best bet is to make page views time out ... this will make inaccuracy's but will still work .. could this be done with ajax ?? im assumption is that it can but im no java-script expert Quote Link to comment https://forums.phpfreaks.com/topic/222795-current-amount-of-users-online/#findComment-1152085 Share on other sites More sharing options...
the182guy Posted December 28, 2010 Share Posted December 28, 2010 No AJAX needed, just setup your system to store PHP sessions in the database, this will mean that you have a sessions tabe and there will be a field for the timestamp of the last request. All you need to do is run a query to pull out the number of sessions (rows) that have a timestamp in the last say 10 minutes. There is a way to increase the accuracy with AJAX, which is to (within your page) call one of your scripts every say 1 minute, which will update the session timestamp, then of course if a user closes the page, the AJAX call will no longer run and the timeout won't be updated, so you can assume the user is not online if the timeout is older than a minute. Personally I won't bother with the AJAX though, I would just look at the table and see who was updated in the last 10 minutes. Quote Link to comment https://forums.phpfreaks.com/topic/222795-current-amount-of-users-online/#findComment-1152119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.