• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1308
  • Last Modified:

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.
0
termlimit
Asked:
termlimit
  • 10
  • 6
1 Solution
 
v2MediaCommented:
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

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

Any ideas?
0
 
v2MediaCommented:
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
termlimitAuthor Commented:
Is there any way to do this without the prototype library?
0
 
termlimitAuthor Commented:
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?
0
 
v2MediaCommented:
What js libraries/frameworks are you currently using on the site?
0
 
termlimitAuthor Commented:
jquery
fckeditor
0
 
v2MediaCommented:
I'll check out jQuery for a similar method. What version u using?
0
 
termlimitAuthor Commented:
jQuery 1.2.6 - New Wave Javascript
0
 
termlimitAuthor Commented:
o and thanks again!
0
 
termlimitAuthor Commented:
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

0
 
v2MediaCommented:
Can you chuck up a link? There's probably a conflict happening..
0
 
termlimitAuthor Commented:
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.
0
 
v2MediaCommented:
How about including the script just before the closing </body> tag and changing

window.onload = inac;

to just

inac();

(purge browser cache before testing)

I'm hoping the conflict is with the window.onload. If that doesn't work, posting the debug output from firefox's js debugger would be handy.
0
 
termlimitAuthor Commented:
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.
0
 
termlimitAuthor Commented:
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now