I recently designed this function to determine how many users are currently online using session information. This included setting the session.save_path directive via .htaccess so that I could be certain that all sessions were related to my website.
The function works like I expect it to. I am curious if this will have any effect on garbage collection. And also if opening many files will have an adverse effect on resource usage. The session files are very small and I only read the first 11 bytes of each file. I haven't noticed any lag in response time up to this point. I *think* that garbage collection is still going on as it should. But I thought it would be helpful to get any input on its design as its not unusual for my site to have upwards of 40,000 hits monthly.
Any input is appreciated!
: )
Rich
Code:
/*
* void count_users([ bool detailed]);
*
* Determine how many users are on the site
* Detailed will create an array of user information including username
*
* TODO make detailed
*/
var $guest_count;
var $member_count;
var $user_count;
function session_count($detailed = false)
{
$sess_dir = opendir($this->session_path);
$this->member_count = (int) 0;
$this->guest_count = (int) 0;
for ($i = 0; false !== ($sess = readdir($sess_dir)); $i++)
{
if (is_file($this->session_path.$this->dir_sep.$sess))
{
$user[$i] = fopen($this->session_path.$this->dir_sep.$sess, 'r', 'b');
$line[$i] = fread($user[$i], 11);
if (stristr($line[$i], "1"))
$this->member_count++;
else
$this->guest_count++;
fclose($user[$i]);
}
}
$this->user_count = $this->member_count + $this->guest_count;
}
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::