Automatic Logout after 15 minutes of inactive in php

后端 未结 8 761
悲&欢浪女
悲&欢浪女 2020-12-04 15:51

I want to destroy session if users are not doing any kind of activity on website. At that time after 5 users automatically redirect on index page. How is it possible? Is pos

相关标签:
8条回答
  • 2020-12-04 16:39

    You may create a cookie for a specific time. For example you could put this on your login page:

    <?php
      setcookie('admin', 'abc', time()+50); 
    ?>
    

    Then in some file part that is included in every page, like 'header.php', you may include:

    <?php
      if (!isset($_COOKIE['admin'])) {
      echo "<script> location.href='logout.php'; </script>";   
      }
    
      setcookie('admin', 'abc', time()+50);
    ?>
    

    In the above example, after 50s the cookie will die and the user will be logged out automatically.

    0 讨论(0)
  • 2020-12-04 16:42
    <form action="index.php" method="post" name="frm"><input name="uname" type="text" placeholder="User Name" />
    <input name="pass" type="password" placeholder="Password" />
    <input name="submit" type="submit" value="submit" /></form>
    In index.php
    <?php if(isset($_SESSION['loggedAt'])) { header('dashboard.php'); } 
    if(isset($_POST['submit'])) { $name=$_POST['uname']; $pass=$_POST['pass']; 
    if($name=="admin" &amp;amp;amp;&amp;amp;amp; $pass=="1234") { 
    session_Start(); $_SESSION['username']=$name; $_SESSION['loggedAt']=time(); header('location:dashboard.php?msg=Welcome to dashboard'); } } ?>
    in dashboard.php
    if(time() - $_SESSION['loggedAt'] > 240) { 
        echo"<script>alert('Your are logged out');</script>";
        unset($_SESSION['username'], $_SESSION['loggedAt']);
        header("Location: " . index.php);
        exit;
    } else {
        $_SESSION['loggedAt'] = time();
    }
    
    0 讨论(0)
提交回复
热议问题