当用户没有活动时超时会话

时间:2017-08-25 16:08:33

标签: php html session

如果用户没有活动,我怎么能超时? 这是我的代码,其名称为session.php:

<?php
  require_once('conn.php');
  session_start();

  $user_check = $_SESSION['login_user'];

  $ses_sql = mysqli_query($conn, "select * from employee where Username = '$user_check'");

  $row = mysqli_fetch_array($ses_sql, MYSQLI_ASSOC);

  $login_session = $row['EmployeeName'];

  if (!isset($_SESSION['login_user'])) {
      header("location:index.php");
  }
?>

1 个答案:

答案 0 :(得分:1)

将此代码包含在您登录后要重定向到的个人资料页面中。首先计算这样的时间。

// CONNECT TO THE DATABASE

 con=mysqli_connect('localhost','XXX','XXX','XXX');
 if (mysqli_connect_errno()) // Check connection
 {echo "Failed to connect to MySQL: " . mysqli_connect_error();  }

 $Pid = $_GET['Pid'];

 $prodquery = mysqli_query($con,"select product_id, coupon_id from   
 oc_coupon_product where product_id = '$Pid' "); 
 while ($prod = mysqli_fetch_array($prodquery)) {

  $code = $prod['coupon_id'];

   $returnquery = mysqli_query($con,"select coupon_id, code, discount  from   
 oc_coupon where coupon_id = '$code' "); 
  while ($show = mysqli_fetch_array($returnquery)) {
    if(isset($show)) {

       echo "variable is set to  $show[code]";

     } else {
       echo "variable is not set";
     }  
   }
 }

这会给你一些时间。在那之后做到这一点。我们假设你想在30秒内计时。这里login.php是注销后重定向的任何常见页面。

<script>
var sec = 0;
var min = 0;
var hrs = 0;

function time () {

    if (sec<=59) {
        sec++;
        if (sec<10) {
        sec="0"+sec;
        }
        if (sec>59) {
            sec=0;
            min++;

            if (min>59) {
                min=0;
                hrs++;

                if (hrs>24) {
                    hrs=0;
                };
            };
        };

    };


    document.getElementById("time").innerHTML=hrs+":"+min+":"+sec;

}
function tick () {
    if (min<10) {
        min="0"+min;
        }

    if (hrs<10) {
        hrs="0"+hrs;
    }
}
window.onload=function () {
    tick();
    setInterval(time,1000);
}

</script>

我希望这会奏效。