0

I want to auto logout from index.php after session expired in 10 minutes. Please help?

I already have this:

//this is login.php
//register the session for user and password
session_register("userName");
session_register("password");
if($userType=="Web_User"){
header("location:index.php?");
} 

//index.php
//check session start or not
    <?php
if (!isset($_SESSION['start_time']))
{
    $str_time = time();
    $_SESSION['start_time'] = $str_time;
}
echo $_SESSION['start_time'];

//here I want to expired if user inactive for 10 minutes and redirect to the login.php

?>
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
sam
  • 439
  • 1
  • 6
  • 11

2 Answers2

0

You could set a php session timeout or hard code it in like this.

Add this to when a user is logged in.

$_SESSION['start_time'] = strtotime("now");

Add this where you want to check if they have elapsed 10 minutes.

if($_SESSION['start_time'] <= strtotime("-10 minutes"))
{
    //Log them out.
}
Community
  • 1
  • 1
ethree
  • 1,582
  • 2
  • 14
  • 21
  • $_SESSION['start_time'] = strtotime("now"); this will show the session already started by echoing , but the second part is not work for me. – sam Jul 17 '13 at 10:57
0

I found this in stackoverflow

        <script>
var timer = 0;
function set_interval() {
timer = setInterval("auto_logout()", 600000);
// set to 10 minutes
}

function reset_interval() {
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove   2. mouseclick   3. key press 4. scroliing
//first step: clear the existing timer

if (timer != 0) {
clearInterval(timer);
timer = 0;
// second step: implement the timer again
timer = setInterval("auto_logout()", 600000);
// completed the reset of the timer
} 
}

function auto_logout() {
 // this function will redirect the user to the logout script
 window.location = "logout.php";
}
</script>

and in the body tag

onLoad="set_interval();" onmousemove="reset_interval();" onclick="reset_interval();" onkeypress="reset_interval();" onscroll="reset_interval();"
  • This is nice working script, I just used it, but will try how to do it with session. – sam Jul 17 '13 at 10:52
  • 1
    Use the above code in index page where your session start and you can destroy the session in logout page ..it reload to index page –  Jul 17 '13 at 14:32