0

As being new to this,i am trying to implement a session timeout after 100 seconds if a user does not click on "any other links" i.e stays on that page without any activity..i used the following php script but i guess the "if" condition check is done only once per script and so the automatic redirection would not occur even if the check gets violated after sometime..so is there a way(using only php script) so that this time check occurs repeatedly on the page??

if (isset($_SESSION['LAST_ACTIVITY']) and (time() - $_SESSION['LAST_ACTIVITY'] > 100)) 
{
    session_unset();                    
    session_destroy();                          
    header('Location: seesiontimeout.php');
}

the $_SESSION['LAST_ACTIVITY'] variable has been set at starting of script...

2
  • what do you have in your $_SESSION['LAST_ACTIVITY'] Commented Dec 1, 2012 at 5:50
  • You could use setTimeout() on the page itself and reset it at any kind of activity. Once it fires you can perform the redirect. You should still check with PHP once anything is submitted though. Commented Dec 1, 2012 at 6:09

2 Answers 2

2

You asked...

.so is there a way (using only php script) so that this time check occurs repeatedly on the page??

(emphasis mine).

No. PHP runs on the server once per request to render dynamic HTML. It can't just run idle and somehow tell the browser, "hey you're about to time out!"

You'll have to set up a client-side javascript function that asks the server repeatedly.

Have I timed out yet? Nope.
Have I timed out yet? Nope.
Have I timed out yet? Yep! Redirect to timeout page.

Sign up to request clarification or add additional context in comments.

Comments

0

is there a way(using only php script) so that this time check occurs repeatedly on the page

Not using "only PHP", but you can use AJAX in JavaScript to keep making calls to the PHP on the server.

PHP runs entirely on the server, it is not executed by the client's browser. As such, you have to tell the client to keep checking with the server to determine things like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.