Jump to content

Logout Inactive User


UniqueHaze

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

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.

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.