php - count all session of users on whole website

380

friends i am developing website for chat . when some user login my script create session

lest example

/* if assume four user on online on my website i want to check all user online according to session   */

/*userone*/ $_SESSION['user-login']=kamran101;
/*usertwo*/ $_SESSION['user-login']=saleem1;
/*userthree*/ $_SESSION['user-login']=asad23;
/*userthree*/ $_SESSION['user-login']=janbran345;

 $_SESSION['user-login']=$name; 

if i user

$total_user = count($_SESSION['user-login']) ;

it is showing only one user so i want to count all user online on user . so i count all user login session

i need help in this aspect if any person can thanks in advance

828

Answer

Solution:

If you only want to know how many users are currently online, try

count(scandir(ini_get("session.save_path")));

This will give you the number of currently active sessions.

OR

you can use this way to find the count

   <?php
session_start();  
    function OnlineUsers() {
   $count = 0;  
       $handle = opendir(session_save_path());
   if    ($handle == false) return -1;  
       while (($file = readdir($handle)) != false) {
         if (ereg("^sess", $file)) $count++;
   }
   closedir($handle);  
       return $count;
}
?>

you can get the value using this way,

$usercount = OnlineUsers();

People are also looking for solutions to the problem: jQuery Script called from within php While loop not working properly

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.