Jump to content

Recommended Posts

Hi,

 

I have been trying to write a function to count the number of users logged in to my site and I think I am amost there, but a couple of things are just defying all logic (at least mine anyways)

 

When the user logs in I set the status of $_SESSION['loggedin'][0] to Logged In and $_SESSION['loggedin'][1]  to the current time - time(). Each time the user enters a members page I update $_SESSION['loggedin'][1] to the current time.

 

In my function I read the session file and subtract the current time from the time in the session file and if it is < 600 it doesn't get counted.

 

Two problems. If I log in and then just shut down and open the brower a few times (creating empty session files as I have session_start() at the top of the login page) it counts the number of files, even though they are empty - it should only be counting the files that match the criteria set out in the function.

 

Second problem. How can I extract just the time from the session file since it is always going to be different. For testing I am using substr, but this won't always be constant as session variables will come and go as the user moves through the site.

 

Hope all this makes sense. I know this isn't an exact science counting logged in users, but if I can even be close I would be happy. If anyone has a better solution or other ideas, I'm open to suggestions........

Here is my function.

 

Thanks

 

 

function getOnlineUsers(){

 

if ( $handle = opendir( session_save_path() ) ) {

while ( false !== ( $folder = readdir( $handle ) ) ) {

if($folder != '.' && $folder != '..'){

$folderarray[] = $folder;

}

}

closedir($handle);

}

 

$count = 0;

$linux = session_save_path()."/";

 

for($i = 0; $i < count($folderarray); $i++) {

$file = fopen($linux.$folderarray[$i], "r");

while (!feof($file)) {

$line = fgets($file);

$contents[] = $line;

}

fclose($file);

if(strstr($contents[0], "Logged In")) {

if(time() - substr($contents[0], -12, -2) < 600) {

$count++;

}

}

}

return $count;

}

You should be using a database.

 

Store the member's id and the last access time in a table. On each page you want to keep track of, either INSERT (if there is not already a record for that id) or UPDATE their existing record. To display how many or even who is on line and since you must clean out old records anyway, just execute a query to delete records older than your time limit and the records remaining are those for members on line.

After reading many articles on this, the concensus was that it may be a strain on MySQL to keep pounding it with updates and reads if your getting a lot of traffic. There could be anywhere from 500 to 1000 users logged in at any given time. This is why I went the way I did.

 

I have since fixed a couple of issues, but just one remains.

 

When a user logs in they are validated and if all is well the sessions are set and the page is refreshed, but for some reason it won't count the user unless the browser is closed and reopened. I have no idea why it is doing this. You can refresh until the cows come home, but nothing - maybe a cache thing????

 

Thanks

 

Opening and parsing through session data files is going to take 100 times more processing time using (slow) parsed/tokenized/interpreted php than executing queries like the following -

 

Add id and mysql timestamp to table -

// INSERT/UPDATE on each page access (table with id and ts (timestamp) columns) -
$query = "INSERT INTO table_name
SET id = $id
ON DUPLICATE KEY UPDATE
ts = NULL"; // must have an UPDATE expression here.
// ... execute query here

 

Get and display how many are on-line -

// DELETE the old records -
$limit = 600; // your "on line" time limit
$query = "DELETE FROM table_name WHERE ts < DATE_SUB(NOW(),INTERVAL $limit SECOND)";
// ... execute query here
$query = "SELECT count(*) FROM table_name"; // get a count of how many are on-line
// ... execute query here and display results

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.