【问题标题】:Automatic Logout after 15 minutes of inactive in php在 php 中不活动 15 分钟后自动注销
【发布时间】:2021-07-27 01:41:28
【问题描述】:

如果用户没有在网站上进行任何类型的活动,我想销毁会话。 当时5个用户在索引页面上自动重定向。这怎么可能? 可以在 php 中进行会话处理,为此我必须维护或更新用户登录时间..

【问题讨论】:

    标签: php


    【解决方案1】:

    在这里使用这个小 sn-p 相对容易实现:

     if(time() - $_SESSION['timestamp'] > 900) { //subtract new timestamp from the old one
        echo"<script>alert('15 Minutes over!');</script>";
        unset($_SESSION['username'], $_SESSION['password'], $_SESSION['timestamp']);
        $_SESSION['logged_in'] = false;
        header("Location: " . index.php); //redirect to index.php
        exit;
    } else {
        $_SESSION['timestamp'] = time(); //set new timestamp
    }
    

    【讨论】:

    • 没有问题,这应该可行,也许你必须调整一些变量,我不知道你使用什么,但总的来说这正是你想要的。
    • 我试过了!但是,它不起作用。我需要修改什么才能使其正常工作吗?我尝试按照您的说法来实现代码。但是,它也说服务器错误。
    • 如果用户没有导航到任何页面,这如何工作?要执行 PHP 代码,必须有一个服务器请求。如果客户端在没有请求的情况下应该超时重定向,只能通过JavaScript来完成……
    • @YUNOWORK 你是什么意思,为什么我会在用户不做某事时注销他?除非您可以访问网络摄像头并准确监控坐在计算机前的人,否则无法区分 “人在未注销的情况下离开计算机”“人正坐着在电脑前什么都不做”。因此,如果网站空闲并且此人没有发出任何请求,只需注销(ergo - 用户没有做任何事情)。银行网站一直都在这样做。在站点内不活动几分钟后,会话就会退出 - 无论我在哪里。
    • $_SESSION['timestamp'] 的初始值为 null (0),因此您最终将无限循环返回 login.php,就好像语句始终为真(大于 900)
    【解决方案2】:

    我从 Sitepoint.com 获得了这个解决方案 在 html 中使用简单的元标记

    <meta http-equiv="refresh" content="900;url=logout.php" />
    

    900 是您希望会话在不活动时终止的时间(以秒为单位)。

    希望对你有用

    编辑:此方法不实现任何其他逻辑,因此只有在您想“强制”注销时才有效,如 cmets 中所述

    【讨论】:

    • 简单有效!好主意。
    • 我应该把元标记放在哪里。我的php代码?每页?
    • 您可以将它放在一个文件中,然后将其包含在所有其他文件中。类似include 'header.php'
    • 每次打开浏览器都会重启
    • 即使您正在使用该页面,这也会将您注销。
    【解决方案3】:

    您可以为特定时间创建 cookie。 例如,您可以将其放在您的登录页面上:

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

    然后在每个页面中包含的某些文件部分中,例如“header.php”,您可以包含:

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

    在上面的例子中,cookie 会在 50 秒后失效,用户会自动退出。

    【讨论】:

      【解决方案4】:

      我的解决方案是 (我给你解决方案,但这种简单的语法没有被尝试过)

      checkerOrCreatorTime.php

      <?php
      //if using the session, this additional advice me
      ini_set('session.cookie_httponly', 1);
      ini_set('session.use_only_cookies', 1);
      session_start();
      //create session (JUST FOR ONE TIME)
      if (!isset($_SESSION['THE SESSION KEY FOR LOGIN (EX. USERNAME)'])){
          //create anyting session you need
          $_SESSION['user']['THE SESSION KEY FOR LOGIN (EX. USERNAME)'] = 'USER';
          $_SESSION['user']['TIME'] = '900';
      }else
      if (time() -$_SESSION['TIME'] > 900){
          unset($_SESSION['user']);
          // and whatever your decision
      }
      ?>
      

      常见问题:

       1. Why use ['user'] is session login?
          if you using many session for user, you just unset one var, like this.
      
       2. why use a ini_set.... in this syntax?
          for more security
      

      如果您喜欢使用现代网络,只需将 javascript 用于 ajax

      【讨论】:

      • $_SESSION['user']['TIME'] 这将为每个用户设置一个单独的变量,但是 $_SESSION 变量已经为每个用户定义了一次,因此您不需要这样做。只需执行 $_SESSION['TIME'] 即可。
      【解决方案5】:
      <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();
      }
      

      【讨论】:

        【解决方案6】:

        此代码包含在connection.php中以确保该代码包含在任何页面中,但您可以在任何您想要的页面上实现

        if (isset($_SESSION['user-session']) OR isset($_SESSION['admin-session']) ) {
        //then we are checking the activity sesssion $_SESSION['']
        if (isset($_SESSION['last_active'])) {
        
            //if the time is set then we check the difference
            $max_time=5*60; #number of seconds
            $now=microtime(date("H:i:s"));
            //Checking the last active  and now difference in seconds
            $diff=round(microtime(date("H:i:s"))- $_SESSION['last_active']); #the difference of time
            if ($diff>=$max_time) { #if the difference is greater than the allowed time!
                //echo "logging out couse the time is".$diff;
                header("location:logout.php");          
            }else {
                $time=microtime(date("H:i:s"));
            $_SESSION['last_active']=$time; #Updating the time 
            //echo 'More time added the time was!'.$diff;
            }
        }else{
            //if there is no last active then we create it over here
            $time=microtime(date("H:i:s"));
            $_SESSION['last_active']=$time;
        }}
        

        【讨论】:

          【解决方案7】:

          使用 .htaccess 的简单解决方案

          将以下行添加到您的 .htaccess 文件中,其中 3600 是秒数。 会话将在一定时间后自动销毁,与活动或不活动无关。

          根据以下代码会话将在 1 小时后销毁。

          php_value session.gc_maxlifetime 3600
          
          php_value session.gc_probability 1
          
          php_value session.gc_divisor 1
          

          【讨论】:

            【解决方案8】:

            这是代码示例。

            session_start();
            $t=time();
            if (isset($_SESSION['logged']) && ($t - $_SESSION['logged'] > 900)) {
                session_destroy();
                session_unset();
                header('location: index.php');
            }else {
                $_SESSION['logged'] = time();
            }                          
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-12-22
              • 1970-01-01
              • 2011-08-04
              • 2016-04-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多