Subject: Counting users online via sessions
Posted By: richard.york Post Date: 11/17/2003 4:03:47 PM
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


        /*
         * 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
:::::::::::::::::::::::::::::::::

Reply By: richard.york Reply Date: 11/19/2003 12:14:12 AM
I was able to correct what was causing me some concerns by upping garbage collection probability.  I was getting output saying I had 40-90 users online at once.  Ever the optimist I didn't believe the output.  It seems that old sessions were piling up.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::


Go to topic 6549

Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997
Return to index page 996
Return to index page 995
Return to index page 994
Return to index page 993