Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2003, 05:03 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default Counting users online via sessions

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
:::::::::::::::::::::::::::::::::
 
Old November 19th, 2003, 01:14 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
users online vantoko BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 February 23rd, 2007 08:29 AM
users online.. ruhin Beginning PHP 0 February 9th, 2005 01:58 PM
Urgent ! Find Online Users qazi_nomi Classic ASP Databases 1 August 17th, 2004 04:39 PM
users online natmaster Beginning PHP 1 July 31st, 2003 02:09 PM
Counting Users returning to a page they filled out bicho Classic ASP Basics 1 July 6th, 2003 03:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.