-3

I had this code but I do not Knew how I can add logout after x minutes of inactive in php

login page:

<html>
<head>
<title>login</title>
</head>
<body>
<form action="page1.php" method="get" >
ID:<input type="text" name="id"  />
Password:<input  type="password" name="pass" />
<input type="submit" value="login" />
</form>
</body>
</html>

get page:

<html>
<head>
<title> get</title>
<body>
<?php
session_start();
$get_id= $_GET['id'];
$get_pass = $_GET['pass'];
$_SESSION['user_id']=$get_id;
$_SESSION['user_pass']=$get_pass;
echo "welcome";
?>
</body>
</html>

now I want this code logout after inactive 10 minutes

John
  • 51
  • 4

2 Answers2

1

You need to use JavaScript to make a counter and for example a redirect after x minutes to the logout page.

Example

<script>
  function logout() {
    window.location.replace("http://example.com/logout.php");
  }
  setTimeout(logout(), 10*60000);
</script>
Fabio Widmer
  • 529
  • 1
  • 7
  • 19
0

This is already answered by @Gumbo here How do I expire a PHP session after 30 minutes?

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 600)) { // last request was more than 10 minutes ago session_unset(); // unset $_SESSION variable for the run-time session_destroy(); // destroy session data in storage } $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

Community
  • 1
  • 1
axcl
  • 410
  • 6
  • 21
  • I used this code before but did not work. – John Jul 03 '16 at 12:36
  • are you trying to logout without refresh then you should go with Javascript and just call your logout function from it as @FabioWidmer mentioned below – axcl Jul 03 '16 at 12:38