douqiao6563 2019-03-13 14:17
浏览 117

停止会话以退出用户

I have some code that I added to try and prevent the user from auto logging out (session). - but it still logs the user after so long

What I want is for the user to be able to access multiple pages when logged in and not log them out if they go idle, hence why I put a large idle time - or in other words , stay logged in until they decide to click logout.

login-page

$result = mysqli_query($connection,$query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
        if($rows==1){
        $_SESSION['username'] = $username;
        $_SESSION['username']=time();
            // Redirect user to index.php
        header("Location: admin-index.php");
         }else{
            $_SESSION['incorrect'] = 'Incorrect email or password. Please try again.';
            header ("Location: admin-login.php?=incorrectlogin");
    }
}

logged in index page

<?php 
session_start();
$idletime=6000000;
if (time()-$_SESSION['username']>$idletime){
    session_destroy();
    session_unset();
    header ("Location: admin-login.php");
}else{
    $_SESSION['username']=time();
}
//on session creation
$_SESSION['username']=time();
?>


<!DOCTYPE html>
<html lang="en" >

<head>

second php page

<?php
session_start();
        if(!isset($_SESSION["username"])){
header("Location: admin-login.php");
exit(); }
?>
  • 写回答

2条回答 默认 最新

  • dqspy04266 2019-03-13 14:36
    关注

    There are a few ways of handling this:

    You could change the lifespan of the sessions on the servers itself or if you can't access the server settings, you could try overwriting it through code:

    ini_set('session.gc_maxlifetime', 6000000);

    session.gc_maxlifetime manual

    If you are unable to change the lifespan of sessions on the server, you could use this small code 'trick', keep in minde that if the clients pc shuts down or goes into sleep mode, the sessions will also expire:

    Basicly you create a php script containing:

    session_start();

    Second you just write some jquery with the following code:

    $(document).ready(function() {
        // keep alive
        setTimeout(keepalive(),120000);
    
    });
    
    function keepalive() {
        $.get( "url/to/ajax.php", function( data ) { setTimeout(callserver,12000);  });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?