Click to See Complete Forum and Search --> : Number of active sessions on server


The Little Guy
09-01-2006, 11:50 PM
How do you count the total number of active sessions?

pcthug
09-02-2006, 12:04 AM
You can count the number of active session with a timeout, every time a user views a page or logs in the time is stored in a database against there name. To calculate the number of active sessions you would count all names that have times within the last 10 minutes.

The Little Guy
09-02-2006, 12:13 AM
So... How would I count?

NogDog
09-02-2006, 12:26 AM
Sessions aren't really "active" on the server. Rather, a file is created fore each session to store the session data until a script requests it. Whenever a script executes a session_start(), a "garbage collection" routine checks for any session files in the directory with a last access time older than the session.gc_maxlifetime configuration value.

The session cookie sent to the client also has an expiration time, but it is independent of the above file lifetime.

So, from the server side, the only way you could track potentially active sessions would be by counting the number of files in the session directory. If you're on a shared server and only want to track your sessions, then you'd probably want to use the session_save_path() function to set your own session directory. Then you could use scandir() or glob() to count the number of files in that directory.

The Little Guy
09-02-2006, 12:43 AM
Here is what I have:

session_start();
$_SESSION['test'] = "Kitty";
session_save_path("$home/save_sessions/");
define("MAX_IDLE_TIME", 3);
$i = 0;
foreach (glob("$home/save_sessions/*") as $filename) {
$i++;
}
echo $i;


Its not working.

NogDog
09-02-2006, 12:53 AM
I believe the session_save_path() needs to come before the session_start().

The Little Guy
09-02-2006, 12:57 AM
I get these 4 errors:

Warning: session_start() [function.session-start]: open(http://localhost/date/save_sessions\sess_7e66f952faf72a3add28a813b7e8ec46, O_RDWR) failed: Invalid argument (22) in C:\Program Files\xampp\htdocs\date\index.php on line 8

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\date\index.php:8) in C:\Program Files\xampp\htdocs\date\index.php on line 8

open(http://localhost/date/save_sessions\sess_7e66f952faf72a3add28a813b7e8ec46, O_RDWR) failed: Invalid argument (22) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (http://localhost/date/save_sessions) in Unknown on line 0

The Little Guy
09-02-2006, 01:09 AM
OK, I changed it to this:
session_save_path($home/save_sessions);
session_start();
$_SESSION['test'] = "Kitty";
define("MAX_IDLE_TIME", 3);
$i = 0;
foreach (glob("$home/save_sessions/*") as $filename) {
$i++;
}
echo $i;

But I still get errors, less but there are errors I don't know what they mean either.

Warning: Division by zero in C:\Program Files\xampp\htdocs\date\index.php on line 7

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\date\index.php:7) in C:\Program Files\xampp\htdocs\date\index.php on line 8

katten
09-02-2006, 02:27 AM
put


ob_start();


at the top

bokeh
09-02-2006, 05:17 AM
put


ob_start();


at the topNot this band aid fix again. Put session_start at the beginning, not ob_start.

Just to expand on the database idea: Create a table...CREATE TABLE `active_sessions` (
`session_id` VARCHAR(32) default NULL,
`last_access` DATETIME default NULL,
UNIQUE KEY `session_id` (`session_id`)
)
Now to keep a record of and retrieve the number of active sessions from your script use the following: $active_sessions = 0;
$minutes = 5; // period considered active
if($sid = session_id()) // if there is an active session
{
# DB connect here
mysql_query("DELETE FROM `active_sessions` WHERE `last_access` < DATE_SUB(NOW(),INTERVAL $minutes MINUTE)");
mysql_query("INSERT INTO `active_sessions` (`session_id`, `last_access`) VALUES ('$sid', NOW()) ON DUPLICATE KEY UPDATE `last_access` = NOW()");
$active_sessions = mysql_num_rows(mysql_query('SELECT * FROM `active_sessions`'));
}

chazzy
09-02-2006, 07:11 AM
little guy, where exactly are you defining $home?

also, you should read the pages on php.net sometimes..they contain good info!


dont forget: if you use session_save_path on the page, that registers a variable, you have also to use session_save_path on all the pages, where you access the session-variable. under win32 you can use the double \\ to specify eg "c:\\temp\\"

NogDog
09-02-2006, 12:24 PM
Division by zero error is because you didn't quote the path in the session_save_path($home/save_sessions); statement, so it's dividing $home by the self-defined constant save_sessions, and is evaluating save_sessions as 0.

The Little Guy
09-03-2006, 12:50 AM
also, you should read the pages on php.net sometimes..they contain good info!

I do read them.

NogDog I think your code works, I do get a result, and currently it says 1.

shane.carr
09-28-2006, 07:07 PM
I have the same problem; what about something like


session_start();
$a = file_get_contents("sessions.txt");
$a = unserialize($a);
$c = array();
foreach($a as $b){
if($b[1]-time()<1000000&&$b[0]!=session_id()){//time() is in miliseconds, right?
array_push($c, $b);
}
}
$sessioncount = count($c)+1;

array_push($c, array(session_id(), time()));
$d = serialize($c);
$h = fopen("sessions.txt", "w");
fwrite($h, $d);
fclose($h);


This is untested, but it's worth a shot.