Jump to content

[SOLVED] Files in directory not 0kb?


NorthWestSimulations

Recommended Posts

Quick question for you guys. I have no clue on how to do this. and I got no clue where to look for help. So if you guys would help me at all by writing some source code or showing me a link to where I could go for information I would be greatly appreciative! How heres my problem:

 

I have a folder in my cgi-bin called tmp. This is where I keep all my session data. Whenever I access my website a session is created that equals a filesize of 0kb. And whenever a session is registered depending on what kind of user they are the file size for the session file is around 1kb to 10kb. So heres my question...

 

How would I make a script to count all the files that are equal to 0kb and store it to a variable $x. Then count all the files that are greater than or equal to 1kb and store the total in a variable $y. So that I could in turn create a thingy that says...

 

There are $x Guests online, and $y Members online.

 

Any suggestions? I hope I made egnouph sense...

Link to comment
Share on other sites

This should do it

<?php
$dir = "cgi-bin/bin"; //FULL PATH
$Guest = 0;
$Members = 0;
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir))
    {
        while (($file = readdir($dh)) !== false)
        {
        	if($file == "." || $file == ".." || is_dir($dir.$file)) continue;
        	$size = filesize($dir.$file);
            if($size > 0)
            {
            	$Guest++;
            }else{
            	$Members++;
            }
        }
        closedir($dh);
    }
}
echo "Members: $Members<br>Guests: $Guest<br>";

Link to comment
Share on other sites

Using the existence of session data files to determine how many guest/members are on line is both inaccurate and inefficient.

 

It's inaccurate because session data files are only removed when session garbage collection GC runs, and by default it runs randomly (to reduce the number of directory listings), so session data files can exist long after a visitor is no longer active on your site. GC also only removes files that are older than session.gc_maxlifetime so you cannot tell any closer than that value how long a go a guest/member was active and shortening the value will have the side affect of prematurely logging your members out.

 

It's inefficient because on each page generation where you display the quantity of guests/members, you are accessing the disk directory, scanning through the files and accessing the file size of each file.

 

You should instead use the traditional method of inserting/updating the last access time in a table of all the visitors (indicate guest/member) and then doing a simple query to get a count of each type that is less in the past than the time limit you pick.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.