Jump to content

[SOLVED] Users Online Help


steviez

Recommended Posts

Hi,

 

On my site when someone logs in my script runs a simple query to change their status to online. The problem i am having is that if users forget to log out then they are constantly displayed as online. How would i stop this?

 

Thanks

Link to comment
Share on other sites

Easiest way to have a column associated with the user that gets updated with the current time, every time they make a request to the server (click a link, submit something, etc..) and then setup a cron job script and run it every x time (like 15 mins or so..however long you want to deem "inactivity" to be considered "logged out") to check the current time with the last active time of users and if it's greater than your x time, flag their status as offline.

Link to comment
Share on other sites

You'll have to use strtotime and then do equation. Basically if columntime > strtotime(columtime, +15 minutes) then update and set online status to offline. (My strtotime isn't perfect I'm recalling it off the top of my head but that's the general idea) Use the cron job to run the php file every 15 minutes after that.

Link to comment
Share on other sites

what I did for my website is when ever any user click any link, I update his online status as 1 and also update his last login time column. Then in a header page (which is included in all pages), I wrote code to set online status 0 to those whose last login time is greater than 5 mins (dats my threshold). So, any user click any link to my site hence update the current online user tally.

Link to comment
Share on other sites

Yeah that's an alternative but thing is, that creates a lot more unnecessary load for your server, and is also dependent on users clicking it.  What happens when you have a dry spell and nobody's there clicking? I guess that kind of falls under the "If a tree falls in the forest and noone is there, does it make a sound?" category: that is, it doesn't matter if it's showing people as "online" if nobody is there to see it...but still. 

 

But hey, if you're happy with that, then go for it.

Link to comment
Share on other sites

For Crayon Violent ... I totally agree with you about the server load, it do actually create unnecessary load on server. But the thing is I know there will be at best 10 ppl at a time in my server. Another major reason is I have windows server and I know very little about running Cron Job running on a windows server... may be Schedule Task would work, but I never tried.

 

Nut I totally oppose ur Forest and Tree theory. I dont know about others, but my goal for having online user function is there so that the viewers can see who are online now. So, if there is nobody there to click any link, what matters if online user count is right or wrong... nobody is going to check that from phpmyadmin !! whenever any user comes to the page, the online user count will be corrected. So nobody ever can see any wrong info there in the page.

 

For NorthWestSimulations...

What i did to my site is when they login it changes the status of the mysql table `online` to `online` = 'yes' and when they logout it changes it to `online` = 'no'. Im sure theres some way to pull the info from where the sesions data is stored but Im not sure how

what happens if user A logs in and then cross the window without logout (I think most user do that unless it is bank account or so)? Then user A will remains online forever unless he comeback and click on logout ... believe me, the chance is very little...

Link to comment
Share on other sites

well the time() function returns the current unix timestamp in seconds so if you take the current timestamp in your db and format it to that, you can do a simple

 

if (($nowtime - $dbtime) > 300) { // 300 is in seconds so that's 5 mins
// change user status
}

 

I went with this way, and my code is as follows:

 

<?php
// Update online users last active, If inactive change status to offline
$dbtime = sql_row("SELECT * FROM members WHERE status = 'Online' AND username = '".$_SESSION['gt_username']."'");
$lastactive = $dbtime['lastactive'];
$nowtime = time();

if (($nowtime - $lastactive) > 300) { // 300 is in seconds so that's 5 mins
mysql_query("UPDATE members SET status = 'Offline' WHERE username = '".$_SESSION['gt_username']."'");
}
?>

 

Thing is its not working :S

Link to comment
Share on other sites

Orrr....  You could just store the last active time in the database, and not worry about CRON or anything.  (Although, if you have a lot of users, a crontab would be better as far as server stress goes.)

 

Then, all you do, is update the timestamp each page load...

 

UPDATE users SET last_active = UNIX_TIMESTAMP() WHERE user_id = <user_id>

 

Then, to find all of the online users (active within 10 minutes), you could just do...

 

SELECT * FROM users WHERE (last_active > UNIX_TIMESTAMP()-600)

 

Or, to find out if a user is online...

 

SELECT last_active FROM users WHERE user_id = <user_id>

 

Then, pull last_active into a variable....  Then...

 

if(last_active > time()-600)

 

 

But this is potentially a tiny bit more stressful than a crontab.  (Gotta go now, but I can explain it later if you want.)

 

 

 

Anyway, that's just another way to do it.  Not saying it's the way I would do it (although I would consider it), but it's just another possibility.

Link to comment
Share on other sites

did you echo out $lastactive and $nowtime to see if they are holding what you expect?

 

Hi,

 

It works if i execute it manually but the cron wont work. Here is what i have:

 

5 * * * * /home/mydir/cron/online_users.php

Link to comment
Share on other sites

When using cron i get this error:

 

<br />

<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent in <b>/home/mydir/public_html/cron/online_users.php</b> on line <b>14</b><br />

<br />

<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/mydir/public_html/cron/online_users.php:14) in <b>/home/mydir/public_html/cron/online_users.php</b> on line <b>14</b><br />

 

My code is this:

 

<?php

// Start The Session
session_start();

// INCLUDE DATABASE INFORMATION
include "../include/database_config.php";

// INCLUDE CLASS/FUNCTION FILES
include "../include/class_database.php";
include "../include/functions_general.php";
include "../include/global_config.php";

// INITIATE DATABASE CONNECTION
$database = new se_database($database_host, $database_username, $database_password, $database_name);

// ENSURE NO SQL INJECTIONS THROUGH POST OR GET ARRAYS
$_POST = security($_POST);
$_GET = security($_GET);

// Update online users last active, If inactive change status to offline
$dbtime = sql_row("SELECT * FROM members WHERE status = 'Online' AND username = '".$_SESSION['gt_username']."'");
$lastactive = $dbtime['lastactive'];
$nowtime = time();

if (($nowtime - $lastactive) > 300) { // 300 is in seconds so that's 5 mins
mysql_query("UPDATE members SET status = 'Offline' WHERE username = '".$_SESSION['gt_username']."'");
}

?>

 

 

Any ideas?

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.