PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1848
Number of posts: 5016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Timestamp - user timeout Posted by Micky01 on 17 Feb 2006 at 12:11 PM
Hi,

I have tried every way I can think of to achieve this but in the end I just cannot come to a conclusion.

I am trying to timeout a user(s) login if they have been inactive for a specified amount of time.
For example, if a user has been logged in for 15 minutes I want to log them out due to inactivity.
As it stands, users log in, and are assigned a session. I want to force the user to re-login if they have been inactive for 15 minutes.

I have tried endlessly but still can't get it to work.

Can anybody suggest a solution for this?

Thank you in advance.
Report
Re: Timestamp - user timeout Posted by tvienti on 17 Feb 2006 at 1:53 PM
I can think of two ways.. server side and client side. Client side, use Javascript and set a 15 minute timer on each page... at the end of it, redirect to some script that logs them out. If they idle on a page, it'll trigger. If they closer their browser out, their session's dead anyhow so you're still accomplished your goal. And, of course, if they move to another page it resets the timer as you'd expect.

But I have JS. I'd say it'd be more appropriately to just timestamp them each time they request a page. This'll be easy if you have some PHP code that's run on every page, if not you'll have to insert it at the top of every page.

Anyhow, on each page hit run a snipper of code that does something like this:

- Read the timestamp of their most recent page hit. If it's > 15 minutes, log them out and handle appropriately
- Otherwise, overwrite it with the current time - this is their new timestamp

Make sense?

T

: Hi,
:
: I have tried every way I can think of to achieve this but in the end I just cannot come to a conclusion.
:
: I am trying to timeout a user(s) login if they have been inactive for a specified amount of time.
: For example, if a user has been logged in for 15 minutes I want to log them out due to inactivity.
: As it stands, users log in, and are assigned a session. I want to force the user to re-login if they have been inactive for 15 minutes.
:
: I have tried endlessly but still can't get it to work.
:
: Can anybody suggest a solution for this?
:
: Thank you in advance.
:

Report
Re: Timestamp - user timeout Posted by Micky01 on 18 Feb 2006 at 3:49 AM
Got it now, thanks.

I have this working now.
Thanks again for the help.
Here's the code I used, may be useful to someone else.

in login.php
-------------------
$current_time = time(); // get the current time
$_SESSION['loginTime']=$current_time; // login time
$_SESSION['lastActivity']=$current_time; // last activity
......

timeout.php (inc in each page)
--------------
$timeout_min = 15; // 15 minutes of inactivity 
$timeout_length = $timeout_min * 60;

if ($current_time - $_SESSION['lastActivity'] > $timeout_length) {
	$_SESSION = array();
	if (isset($_COOKIE[session_name()])) {
	   unset($_COOKIE[session_name()]);
	}
        session_destroy();
        echo "You have been logged out.......
exit;
}
else
$_SESSION['lastActivity'] = $current_time;

?>


---

: I can think of two ways.. server side and client side. Client side, use Javascript and set a 15 minute timer on each page... at the end of it, redirect to some script that logs them out. If they idle on a page, it'll trigger. If they closer their browser out, their session's dead anyhow so you're still accomplished your goal. And, of course, if they move to another page it resets the timer as you'd expect.
:
: But I have JS. I'd say it'd be more appropriately to just timestamp them each time they request a page. This'll be easy if you have some PHP code that's run on every page, if not you'll have to insert it at the top of every page.
:
: Anyhow, on each page hit run a snipper of code that does something like this:
:
: - Read the timestamp of their most recent page hit. If it's > 15 minutes, log them out and handle appropriately
: - Otherwise, overwrite it with the current time - this is their new timestamp
:
: Make sense?
:
: T
:
: : Hi,
: :
: : I have tried every way I can think of to achieve this but in the end I just cannot come to a conclusion.
: :
: : I am trying to timeout a user(s) login if they have been inactive for a specified amount of time.
: : For example, if a user has been logged in for 15 minutes I want to log them out due to inactivity.
: : As it stands, users log in, and are assigned a session. I want to force the user to re-login if they have been inactive for 15 minutes.
: :
: : I have tried endlessly but still can't get it to work.
: :
: : Can anybody suggest a solution for this?
: :
: : Thank you in advance.
: :
:
:
Report
Re: Timestamp - user timeout Posted by RuntimeTerror on 27 Feb 2006 at 4:54 AM
The JS-approach has the following great disadvantage:
Assume the user logged in and opens multiple browsers in his session. While he'd be using one window intensively, another window that idles would log him out after 15 minutes ;)

And, if it's been for security issues, javascript is least choice.

Nevertheless, I strongly agree with the server-side approach.

RuntimeTerror
The only action that causes an error is trying to do things right!





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.