newbtophp Posted August 18, 2010 Share Posted August 18, 2010 Hey! I have a table...with a column isOnline which is set to 1 when logging in (using login.php)...and 0 when logging out (logout.php), its how I determine whos logged in and whos not (in statistics etc.) Now the problem is if they dont have the remember me enabled..and don't log themselfs out via logout.php (which sets the isOnline to 0)...and close their browser the $_SESSIONS's get destroyed (which means their physically not logged in)...and their isOnline remains at 1 (even though their logged out) Another problem..is if they login with remember me enabled (as they want to remain logged in)....and close their browser or whatever, isOnline will still remain as 1 (theirfore others will think their online). For your info: In login.php I do an UPDATE query adding the current time generated by time() into the loginTime column...and also setting isOnline to 1. So my question is how can I overcome this, I have the isOnline (1 = logged in, 0 not logged in) and loginTime (which contains the login timestamp)? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/ Share on other sites More sharing options...
MadTechie Posted August 18, 2010 Share Posted August 18, 2010 Don't use a 1 for logged in, use a timestamp and update that to the current time on page activity, to see who is logged in, just do a query on isOnline for records with a timestamp of the last 15 minutes.. and that's who's logged in Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101002 Share on other sites More sharing options...
newbtophp Posted August 19, 2010 Author Share Posted August 19, 2010 Ok I've deleted the isOnline as its uneeded. So now I have loginTime which is UPDATEd with the current time() (timestamp) upon page load (its added at the top of every page within an include). How would I determine if a specific user is online, how would the query be (im guessing it would be structured like the example below but not sure how the WHERE bit will be)? SELECT user FROM site_table WHERE loginTime IS WITHIN 15 MINUTES FROM CURRENT TIME AND user = 'tester' Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101023 Share on other sites More sharing options...
MadTechie Posted August 19, 2010 Share Posted August 19, 2010 try this SELECT user FROM site_table WHERE loginTime > (now( ) - ( 15 *60 )) AND user = 'tester' 15 = number of minutes EDIT: Erm.. infact it should be SELECT user FROM site_table WHERE loginTime > (now( ) - ( 15 *60 )) as you want all users not just tester Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101026 Share on other sites More sharing options...
newbtophp Posted August 19, 2010 Author Share Posted August 19, 2010 try this SELECT user FROM site_table WHERE loginTime > (now( ) - ( 15 *60 )) AND user = 'tester' 15 = number of minutes Thanks im going to try it, but wouldn't that mean the loginTime is more then 15 minutes, don't we want it to be loginTime <= 15 (to be within 15 minutes?) or am I totally confused and really dumb Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101028 Share on other sites More sharing options...
MadTechie Posted August 19, 2010 Share Posted August 19, 2010 I am removing time from Now now the user, i could of added the time to the user (either works) here is the logical break down EDIT: (think of is as minutes past 12) if loginTime = 20minutes and Now = 30minutes then loginTime > (Now - 15) = 20 > (15) = true if loginTime = 10minutes and Now = 30minutes then loginTime > (Now - 15) = 10 > (15) = false Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101031 Share on other sites More sharing options...
newbtophp Posted August 19, 2010 Author Share Posted August 19, 2010 I am removing time from Now now the user, i could of added the time to the user (either works) here is the logical break down EDIT: (think of is as minutes past 12) if loginTime = 20minutes and Now = 30minutes then loginTime > (Now - 15) = 20 > (15) = true if loginTime = 10minutes and Now = 30minutes then loginTime > (Now - 15) = 10 > (15) = false Ok great, but one thing wouldn't now( ) be UNIX_TIMESTAMP() instead or $time ( and $time is assigned to $time = time(); )? Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101032 Share on other sites More sharing options...
MadTechie Posted August 19, 2010 Share Posted August 19, 2010 Now would be a UNIX_TIMESTAMP but all dates and times in MySQL are stored as UNIX_TIMESTAMP's and just converted for display, also if you set the time via MySQL then you should use Now() if you set it via PHP time() then you could use time() Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101036 Share on other sites More sharing options...
newbtophp Posted August 19, 2010 Author Share Posted August 19, 2010 Now would be a UNIX_TIMESTAMP but all dates and times in MySQL are stored as UNIX_TIMESTAMP's and just converted for display, also if you set the time via MySQL then you should use Now() if you set it via PHP time() then you could use time() loginTime is set using php's time()... so for checking the total users online, would the following work? <?php $time = time(); $result = mysql_query("SELECT user FROM site_table WHERE loginTime > ($time - ( 15 *60 ))"); //mysql_num_rows etc.. Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101037 Share on other sites More sharing options...
newbtophp Posted August 19, 2010 Author Share Posted August 19, 2010 Now would be a UNIX_TIMESTAMP but all dates and times in MySQL are stored as UNIX_TIMESTAMP's and just converted for display, also if you set the time via MySQL then you should use Now() if you set it via PHP time() then you could use time() loginTime is set using php's time()... so for checking the total users online, would the following work? <?php $time = time(); $result = mysql_query("SELECT user FROM site_table WHERE loginTime > ($time - ( 15 *60 ))"); //mysql_num_rows etc.. Tested that and that works. solved. cheers MadTechie Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101045 Share on other sites More sharing options...
MadTechie Posted August 19, 2010 Share Posted August 19, 2010 yay your welcome Quote Link to comment https://forums.phpfreaks.com/topic/211120-crisis-when-using-cookies-or-not-using-logoutphp/#findComment-1101048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.