smithmr8 Posted February 19, 2008 Share Posted February 19, 2008 Hi, Im using sessions on my website. I was wandering if there was a way I can find out if someone is logged in. Thanks. Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 19, 2008 Share Posted February 19, 2008 see this thread: http://www.phpfreaks.com/forums/index.php/topic,148226.0.html it explains the method with which you would achieve a "users currently online" list. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks Alot. Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 19, 2008 Share Posted February 19, 2008 the other thread has been marked as solved - it probably won't gain replies. there is a script running every five minutes, but not in the background. it is run by the page when the user accesses it. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Ah, ok. I dont see how its going to update if the user has gone offline or AFK. Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 19, 2008 Share Posted February 19, 2008 it doesn't update the user if he has gone offline or AFK - that is the whole point. his last action time will be MORE than 5 minutes ago if he has done either of these things. therefore selecting the list of users with last action times that are more recent than 5 minutes ago ensures that your list is accurate (to 5 minutes). Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Yeah, I get that part. I mean, the code which checks the table and removes those entries who are not active. OR, do the entries stay there for each user, and just have the time-stamps update the 'last_active' field ? Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 19, 2008 Share Posted February 19, 2008 there's no harm in leaving the entry for each user, since it will simply be re-inserted the next time the user visits the website anyhow. furthermore, not deleting those rows gives you one more valuable piece of info: the last time they WERE active on the website. all you need to do is update their timestamp if it's older than whatever interval you choose (here, 5 minutes). Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Yeah, that would be pretty useful for what I need it for . How would I go about checking whether the timestamp in the Table was less than or greater than 5 minutes old ? Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 19, 2008 Share Posted February 19, 2008 1 hour example........ <?php $now = time(); $lastLogin = strtotime($row['lastLogin']); $diff = $now - $lastLogin; $now = date('YmdHis',$now) if($diff > 3600) { // 3600 seconds is 1 hour $query = 'UPDATE members SET logins = logins + 1, lastLogin = '.$now.' WHERE memberID = '.$SEC_ID; mysql_query($query); } ?> Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Ah, I see. Thanks alot. I'll give it a go and let you know how it goes. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 I'm trying to get it to insert the base values into the Table if it is their first time logging in, but there appears to be something wrong with my mysql query, but I dont know what it is. If I comment out the query, the page loads. If I dont, it just shows a blank page. <?php $user = $_SESSION['myusername']; $info="SELECT * FROM users WHERE username='$user'"; $result_info = mysql_query($info); $x=mysql_fetch_array($result_info); if($x['logins'] == 0) { $ip = $REMOTE_ADDR; $timestamp = date("d/m H:i:sA"); mysql_query= "INSERT INTO `online_users` (`userid`, `ip`, `last_active`) VALUES ('$x[iD]', '$ip', '$timestamp')" or die(mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Ok, I've got the 'Last Active' working now. Im just having a bit of difficulty getting this part to work. Its showing the 'Online Now' image for everyone, even though they were last active more than 5 minutes ago. I dont know whether its the way I've stored the timestamp ? <?php $lastLogin = $view2['last_active']; $now = date("d/m H:i:sA"); $diff = $now - $lastLogin; if($diff > 300) { // 3600 seconds is 1 hour echo "<img src=offline.png><br>"; } elseif ($diff < 300) { echo "<img src=online.png><br>"; } ?> Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 How do you find the time difference between two dates/times ? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 19, 2008 Share Posted February 19, 2008 If the two dates and times are timestamps, then you just subtract them and that'll give you the difference in number of seconds. If they're in another format, such as 'yyyy-mm-dd h:i:s', then look into one of MySQL's date functions, specifically DATEDIFF(): http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Yeah. I have two timestamps. Both in this format, 19/02 15:09:33PM. I am trying to figure out how to calculate the difference in seconds between the two. But.. I can seem to find any working solutions to do it. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 19, 2008 Share Posted February 19, 2008 Timestamps are just numbers of seconds, neither is in any format until you format them with a MySQL function or a call to PHP's date(). So perform your calculation before you convert them. Or convert them back to timestamps with PHP's strtotime(). Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Oh. Im having trouble doing any of that at the moment. I have tried using something like the code which was suggested before, but it didn't work. Everything I try now, just doesn't seem to work either. I have a timestamp which was stored in a Table. That is supposed to be used, along with the current time to determine the time difference between the two, preferably in seconds.. which I can then use to determine whether or not the user is still online. $view2['last_action'] = Stored Time Stamp, stored in the format.. ("d/m H:i:sA") I could really use a hand with this. Thanks. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 19, 2008 Share Posted February 19, 2008 Which MySQL data type did you give the column last_active? If it's anything other than DATETIME or TIMESTAMP then you goofed it up. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Gah. Its VARCHAR . I'll change that to timestamp. I could really do with a hand on the code. Which finds the difference and then gives you a value in seconds for the time difference. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 19, 2008 Share Posted February 19, 2008 You should read the MySQL documentation on TIMESTAMP columns to figure out exactly how they work, but from what I recollect, the first timestamp column of a table will auto-update to the current timestamp any time you insert or update a record. Otherwise you want to change your code from finding the TIMESTAMP in PHP and just use MySQL's NOW() function: // I've dropped your $timestamp variable mysql_query= "INSERT INTO `online_users` (`userid`, `ip`, `last_active`) VALUES ('$x[iD]', '$ip', NOW())" or die(mysql_error()); Then if you want all the users online in the past 5 minutes: SELECT * FROM `online_users` WHERE NOW() - `last_active`>=300 Do you know how to use JOINs to connect the usernames to their IDs in the online_users table? Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 Cool, Thanks. JOINs ? ??? Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted February 19, 2008 Author Share Posted February 19, 2008 haha! I've got it working! Thanks alot. Its taken a while, but its great to have it working at last. Cheers! Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 19, 2008 Share Posted February 19, 2008 although this is solved, i want to point out that i was referring to the use of a UNIX timestamp in all situations. formatted dates are not at all necessary when all one needs to do is use them in calculations. Quote Link to comment 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.