-2

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

  • I don't think it will be as simple as using the $_SESSION property, especially since that is a per-user object (e.g., My $_SESSION instance will not have any information about anyone else's $_SESSION instance). This may be a duplicate-ish question:http://stackoverflow.com/questions/42041599/how-to-count-sessions-to-display-total-number-of-logged-in-users-on-the-site – pacifier21 May 14 '17 at 04:54
  • Read please: http://stackoverflow.com/questions/11931401/php-session-for-multiple-users-at-once – Pablo Cesar Cordova Morales May 14 '17 at 04:56
  • you could count the session files –  May 14 '17 at 04:56
  • Looks like a fairly common question. It's here too: http://stackoverflow.com/questions/17259212/finding-total-number-of-active-sessions – pacifier21 May 14 '17 at 04:57
  • 1
    Possible duplicate of [Finding total number of active sessions](http://stackoverflow.com/questions/17259212/finding-total-number-of-active-sessions) – vv01f May 14 '17 at 05:28
  • when some person login there is a session set by user id $_SESSION['user-login'] . i want to count all $_SESSION['user-login'] – liimra software May 14 '17 at 06:00

1 Answers1

0

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();
Vimukthi Guruge
  • 285
  • 3
  • 15