Accessing Session Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shaneiadt
Forum Newbie
Posts: 10
Joined: Sat Mar 15, 2008 9:26 pm

Accessing Session Files

Post by shaneiadt »

Ok first i'll explain what i am trying to do and then how much i have working. I have a php page that prints out the username of each session online (username session var created once the user logs in). Just like a page to show who is online by reading the sessions currently in the specified folder on the server and printing out one of the session variables stored in each file. This works fine........until i add session_start() to the top of the php page and then it doesn't work at all. Here is the code that accesses the folder the sessions are stored in and prints of one of the variables:

Code: Select all

    $dir = "C:/xampp/tmp";
    $files = scandir($dir);
    $allSessions = "";
    $c = false;
 
 
    for($x = 0; $x != count($files); $x++){
 
        $pos = strpos($files[$x],"sess_");
 
        if($pos === false){}else{
            $filehandle = fopen ('C:/xampp/tmp/'.$files[$x], 'r');
            $sessiondata = fread ($filehandle, 4096);
            fclose($filehandle);
            session_decode($sessiondata);
 
            $sessiondata = explode(";",$sessiondata);
 
            $searchString = trim($sessiondata[0]);
            $findMe = 'uname';
 
            $pos = strpos($searchString, $findMe);
 
            if($pos === false){}else{
 
                $commaPos = strpos($searchString, '"');
                $searchString = str_replace('"','',$searchString);
                if(substr($searchString, ($commaPos))!=""){
                    echo "<a href='#'>".substr($searchString, ($commaPos))."</a>,";
                }
 
            }
 
        }
 
    }
This works fine no problem but when i add session_start() to the top of the php file it echo's nothing to the screen. Can someone tell me why this is happening and help find a way around this problem? Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Accessing Session Files

Post by John Cartwright »

I was not able to reproduce the error, although my spider senses tell me there is a better way to handle this.

Typically to count the number of logged in users, you have a lastactive column in the database that gets updated on every request, or via ajax polling (or both). Simply check whos been active within last x minutes.

If you are relying on the files, you might want to investigate how often your garbage cleanup occurs as well.
Post Reply