Jump to content

User, Online, Offline Status


bcooperz

Recommended Posts

Hey Guys/Girls, Thanks for offering to help!

 

I'm currently setting up a small social network for school and I just basically want to know whether the way I'm dealing with in-active users is appropriate and not going to SLOW down my code A LOT,

My method is :

 

3 Functions:

 

function update_active_user(){
global $connection;
global $id;
	$time = time();
	$result3 = mysql_query("UPDATE users5 SET last_update = '$time' WHERE id = '$id'", $connection);
}
function update_inactive_users(){
global $connection;
	$time_to_expire = time() - 300; // 300 seconds off the current time
	$result2 = mysql_query("UPDATE users5 SET online='0' WHERE last_update < $time_to_expire AND online='1'", $connection);
}

function update_active_users(){
global $connection;
	$time_to_expire = time() - 300; // 300 seconds off the current time
	$result2 = mysql_query("UPDATE users5 SET online='1' WHERE last_update > $time_to_expire AND online='0'", $connection);
}

 

First function updates the user's field called last_update to the current time

(This will only occer when the script loads and the user has done something on my website, it will update their last_update field to current time)

 

Second function sets ALL users that haven't loaded any pages in the last 300 seconds to offline or field online to 0, which obviously means the user is offline

 

Third function sets ALL users that have loaded pages in the last 300 seconds to online if they're offline

 

The reason I'm worried about this is not because it doesn't work, it's working fine at the moment with 2 users, I'm worried when their might be A LOT of users and there are people who are active.

 

My question is : will it make my scripts really slow or be bad for my database in ANY way if I do use these functions for ALL users because each time it searches the table, it is searching ALL users to see if they're active or inactive.

Link to comment
Share on other sites

what about users that are on your website and currently doing something else atm.. meaning they are inactive for more than 30 seconds but still on your website/online..?

 

Also, I wouldn't recommend using global calls to variables..

Link to comment
Share on other sites

what about users that are on your website and currently doing something else atm.. meaning they are inactive for more than 30 seconds but still on your website/online..?

 

Also, I wouldn't recommend using global calls to variables..

 

It's 300 seconds not 30 seconds and the globals are not going to change regardless so I'm not to worried about that, also, the user isn't logged out if the field says they are offline, it will just say they're offline, as soon as they do something it will say they're back online.

I really just want to so if it's safe to use these functions with a lot of users, will it slow down my website ETC?

Link to comment
Share on other sites

You can drop two of those functions; you know if the user is online or offline based on their last_active stamp. Running two separate updates (requiring full table scans unless you add an index on the online column) on every request, to update a column which is essentially duplicate data, doesn't make much sense. It's like storing a date of birth and age. Plus in one of your queries you're scanning the table with the WHERE condition you would need to populate the list of online users anyway.

 

Edit

 

Don't take that to mean "add an index on the 'online' column" - drop it. Work out users purely based on their last active stamp. Don't add an index to that either, as you'll be constantly updating it which defeats the purpose of an index.

Link to comment
Share on other sites

You can drop two of those functions; you know if the user is online or offline based on their last_active stamp. Running two separate updates (requiring full table scans unless you add an index on the online column) on every request, to update a column which is essentially duplicate data, doesn't make much sense. It's like storing a date of birth and age. Plus in one of your queries you're scanning the table with the WHERE condition you would need to populate the list of online users anyway.

 

Edit

 

Don't take that to mean "add an index on the line column" - drop it. Work out users purely based on their last active stamp. Don't add an index to that either, as you'll be constantly updating it.

 

Ok, so what your telling me to do here is drop the 'online' column and purely check the user's timestamps to see how long ago they were active

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.