UniqueHaze Posted June 14, 2020 Share Posted June 14, 2020 Hello, I'm currently storing a timestamp of the users last activity in my database in which if that hasn't been updated for 15 minutes (for testing doing 2 minutes) I want it to log the user out. I have been trying different things but they all seem to log me out even though they shouldn't be. Example of something I've tried $Online = time() - 120; if ($CheckOnline['lastaction'] < $Online){ header("Location: Logout.php"); session_destroy(); } Am I going at this the wrong way.? If I do $Online < $CheckOnline['lastaction'] it keeps me logged in but never logs me out. Any help would be brillaint. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/ Share on other sites More sharing options...
requinix Posted June 14, 2020 Share Posted June 14, 2020 Is lastaction a date string or a integer timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578893 Share on other sites More sharing options...
UniqueHaze Posted June 14, 2020 Author Share Posted June 14, 2020 8 minutes ago, requinix said: Is lastaction a date string or a integer timestamp? Its a string, but now you've mentioned that i feel it should be a timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578894 Share on other sites More sharing options...
requinix Posted June 14, 2020 Share Posted June 14, 2020 It can be whichever. Some people prefer date strings, some people prefer timestamps. It doesn't matter very much which you use. ...as long as you use it consistently. If lastaction is a date string then you need to work with other dates as strings as well. Or if you made it a timestamp then you'd need to work with other date as timestamps. That's what your problem is right now: lastaction is a date string but $Online is a timestamp. You can't compare the two directly like that. Either you convert $CheckOnline[lastaction] into a timestamp if (strtotime($CheckOnline['lastaction']) < $Online){ (which you could instead do in your query with a slightly different method) or you convert $Online into a date string using the same format as lastaction if ($CheckOnline['lastaction'] < date('Y-m-d H:i:s', $Online)){ (which you could instead do when you first calculate $Online). Decide whether strings or timestamps make more sense to you, then adjust accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578895 Share on other sites More sharing options...
UniqueHaze Posted June 15, 2020 Author Share Posted June 15, 2020 I've just been playing around with it all, I've still got it as a string in the Database so been trying with strtotime() although its still just sending me to the logout page without logging me in Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578911 Share on other sites More sharing options...
requinix Posted June 15, 2020 Share Posted June 15, 2020 What's your current code? And just in case, what's an exact example of one of the date strings? Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578917 Share on other sites More sharing options...
UniqueHaze Posted June 16, 2020 Author Share Posted June 16, 2020 I managed to get it working but working out the difference in the time then just see if its greater or not. $MaxTime = 1200; $Dif = $Time - $CheckOnline['lastaction']; // echo $Dif; if ($Dif > $MaxTime){ header("Location: Logout.php"); } An additional thing I'm trying is as soon as they "Logout" it deletes the record from the Database. Although if the user just closes the web browser it doesn't delete the entry so I thought about running a Cron Job every 5min / 10min or so which will check the last action time and if its not move in say 30min it automatically deletes that record. I've got the same code todo it but on the delete query I'm not sure what the Where lastaction = will be? So: $MaxTime = 60; // 1200 normal $Dif = $Time - $CheckOnline['lastaction']; // echo $Dif; if ($Dif > $MaxTime){ $sql4 = "DELETE * FROM UsersOnline WHERE lastaction => :last"; $stmt4 = $pdo->prepare($sql4); $stmt4->bindValue(':last', $CheckOnline['lastaction']); $stmt4->execute(); } My value for last isn't right at the moment but not sure how to define what rows to delete? Sorry if thats not to clear. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578940 Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 Do not delete data. It's really, really good to have data, and you cannot magically restore the data when you realize that it was actually useful after all. There's a much simpler answer here: the users online are the ones who've been active in the last X minutes. That's all there is to it. You don't have to delete anything. Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578944 Share on other sites More sharing options...
UniqueHaze Posted June 17, 2020 Author Share Posted June 17, 2020 Yeah I understand that, I'm just making a simple chat room thing. Wanted to get myself into PHP abit more so I just thought about a random project. I know PHP isnt really correct for it but its never going to be hosted more just for the learning side of it all, so the idea was when they haven't been active for the half hour the Cron runs and deletes the users from the table so if needed another user could use the same name Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578969 Share on other sites More sharing options...
gizmola Posted June 17, 2020 Share Posted June 17, 2020 Functionally, it is true that you could use a string or a timestamp to store your times. You should use a timestamp. It works better from a database standpoint, since you can do queries to find timestamps <>= NOW() or some arbitrary time. Also a timestamp takes 4 bytes, whereas a varchar takes as many bytes per row as needed to store the string one for each character, so -- 'mm-dd-yyyy hh:mm:ss' 19 bytes vs. 4 per row? String comparisons are done character by character, which might not work the way you expect, whereas mysql datetime/timestamp types work exactly as you would expect. In your case you would not need to pass anything into the query -- just determine the offset interval you want and encode that into the static query. Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578973 Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 And to be clear about potential confusion, the word "timestamp" has two meanings. The one I was using is a Unix timestamp, which is the number of seconds since January 1st 1970. So it's a number. The one gizmola is using is the TIMESTAMP type in the database, which converts easily between the numeric Unix timestamp and date strings. Quote Link to comment https://forums.phpfreaks.com/topic/310937-logout-inactive-user/#findComment-1578979 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.