Link to home
Start Free TrialLog in
Avatar of termlimit
termlimitFlag for United States of America

asked on

CakePHP redirect a user after idle for a preset length of time

When a user is on my site for x minutes I want them to get redirected to a different page on the site.  A user logs into the site and winds up on the homepage, if they sit there to long I want to redirect them to a temporary page to reduce the load on the server.  Is there an easy way to implement this in CakePHP?  Currently I am using a lot of AJAX on the homepage to interact with the site and would like to implement it within those calls already being made.  Any ideas on this would be helpful as I am stuck.  I can make it work, but it is only working after the session expires and the person goes to another page.  I want it to work automatically based on them being idle on one page and separate from the session time.
Avatar of v2Media
v2Media
Flag of Australia image

How about a Javascript solution? Since this concerns client side activity, it needs a client side solution:-
var idleTime    = 20*60*60*1000; /*20 minutes in milliseconds*/
var timeOut     = '';
var redirectURL = 'http://www.domain.com/page.html';

function init() {
    
    Event.observe(document.body, ‘mousemove’, resetIdle, true);   
    setIdle();   
}

function onIdleFunction(){
   
    window.location.href=redirectURL;       
}

function resetIdle(){
   
    window.clearTimeout( timeOut );
    setIdle();   
}

function setIdle(){
   
    timeOut = window.setTimeout( "onIdleFunction()", idleTime );   
}

Event.observe(window, ‘load’, init, false);

Open in new window

Avatar of termlimit

ASKER

Thanks for the response.  So far all I am getting in response is:
"Event.observe is not a function"

Any ideas?
Sorry - should have mentioned Event.observe uses the protptype library. You can download it here:-

http://www.prototypejs.org/download

Script was written with 1.5.0 version. Download it and include it on your webpages.
Is there any way to do this without the prototype library?
I added in prototype (1.5.0) from the download page you provided.  I made sure that i uploaded it and pointed to the file, before making the call you pasted above.  It is still throwing the same error as before.  It is also breaking the rest of my JavaScript that I have on the page from my current program.  Any way to do this without prototype?
What js libraries/frameworks are you currently using on the site?
jquery
fckeditor
I'll check out jQuery for a similar method. What version u using?
jQuery 1.2.6 - New Wave Javascript
o and thanks again!
Here is what I came up with.  The problem with it though is that at the timeout amount it is redirecting regardless of mouse movement or not.  I want the timer to reset every time a user moves their mouse.  So at 5 minutes in I move my mouse, it should reset and give me 10 more minutes.
<script type="text/javascript">
	/*var idleTime    = 1*60*60*1000; /*20 minutes in milliseconds*/
	var idleTime	= 900000;
	var timeOut     = '';
	var redirectURL = 'http://www.bidash.com/page/idle';

	function inac() {
		$(document).bind('onmousemove', resetIdle);
		setIdle();
	}

	function onIdleFunction(){
		window.location='/page/idle';
	}

	function resetIdle(){
		window.clearTimeout( timeOut );
		setIdle();
	}

	function setIdle(){
		timeOut = window.setTimeout( "onIdleFunction()", idleTime );
	}

	window.onload = inac;
</script>

Open in new window

Can you chuck up a link? There's probably a conflict happening..
Sorry so late getting back to you.  It is a private development server on a closed intranet.  I am thinking that it might be the inac() call also calling the setIdle().  Should that be in there?  Or is the way that I converted it not properly making the timer reset?  I have firebug installed and I can get you any information you need from there.
ASKER CERTIFIED SOLUTION
Avatar of v2Media
v2Media
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I put it just before the </body> tag, changed window.onload = inac; to inac();, and cleared my cache, but I got the same result.  No errors are being thrown by the debugger.  I will try and put it up on a public ftp site for you to look at on a blank page to see if that works at least.

OK here is a test page.  Just this script is running and the mousemove is not registering.
http://www.elementmediadesign.com/test/default.php

I have it set to 9 seconds so you can see the effect without waiting 10 minutes.
Wow completely messed that up!  So the line that I did the bind in, I changed it to the JS call of onmousemove, but it should be mousemove.  I changed that and it was all fixed!  Thanks a lot.  I appreciate the help.