I have a blog/forum for the community i live in and i am looking for help is writing a simple code to echo total number of users logged in at any given time.
each user must login with user name and password and i use session cookie to echo on their page 'logged in as " "'. i want to add total number of users logged in.

thanks for your help
JBWebWorks

    First you need to decide how you want to define "logged in", since such a web application is non-persistent. Probably the most typical method is to update a datetime or timestamp column in the users table with the current time whenever a user accesses a login-controlled page. Then you arbitrarily pick some amount of time that constitutes being "logged in". Then just query the users table for a count of rows where that timestamp is greater than the current time minus that logged in time period.

      could add a column your user database to keep track of if an individual user is logged in then just query it and return the number of rows affected:\

      mysql_query("SELECT uid FROM user_table WHERE loggedin='1';");
      $numloggedin = mysql_affected_rows();

      mysql_affected_rows() gets the number of rows the last mysql_query affected (whether it by SELECT UPDATE any other MySQL query)

      You could also go further and add a script on each page that keeps track of each user's activity to combat someone closing their browser without logging out.

      mysql_query("UPDATE user_table SET lastactivity = '".time()."' WHERE userid='".$userid."';");

      then then you could adjust how long since you want your user's last action to consider them non active

      mysql_query("SELECT uid FROM user_table WHERE loggedin='1' AND lastactivity > '".time()-3600."';");
      $numloggedin = mysql_affected_rows();

      time() returns number of seconds since the unix epoch (Jan 1st 1970) so you'd just time() - 3600 to get all activity within the last hour

      PS: none of that sql is tested so there may be some errors but you should get the general direction.

      Edit: time() returns a 10 digit integer so INT(10) would work in a mysql database and i think modern versions of mysql consider bools to be INT(1) so they will return a 1 or 0 to indicate true or false

        The problem with "Logged In" users is that many of them don't "Log Out". Therefore, your numbers are always going to be inflated, even when accounting for multiple instances a an account being logged in. Plus, what if two or more people are logged in with the same account? How do you count it? How would you know?

        Nog Dog has a pretty good solution for approximating how many people are logged in, but getting an absolute number isn't likely to be possible. One alternative is to force only one instance of a login id at one time. If someone logs in from a second location, it will bump out the previous login. Then, you could query login/logout timestamps, and see how many people logged in within the last hour, half hour, two hours, etc.

          Write a Reply...