当页面空闲时如何在php中设置空闲会话

当页面空闲时如何在php中设置空闲会话,php,Php,login.php文件 <?php session_start(); ?> <?php if($_POST['submit1']) { $v1 = "FirstUser"; $v2 = "MyPassword"; $v3 = $_POST['text1']; $v4 = $_POST['pwd']; if($v1 == $v3 && $v2 == $v4) { $_SESSION['luser'] = $v1; //---

login.php文件

<?php
session_start();
?>
<?php
if($_POST['submit1'])
{
  $v1 = "FirstUser";
  $v2 = "MyPassword";
  $v3 = $_POST['text1'];
  $v4 = $_POST['pwd'];
  if($v1 == $v3 && $v2 == $v4)
  {
    $_SESSION['luser'] = $v1;
    //-----------------------------------------------

    $_SESSION['start'] = time(); // taking now logged in time
    $_SESSION['expire'] = $_SESSION['start'] + (5 * 60) ; // ending a session in 30     minutes from the starting time
    header("location: http://127.0.0.1/session_exmpl/home.php");

    //-----------------------------------------------
  }
  else
  {
    echo "Please enter Username or Passwod again !";
  }

}
?>
<html>
<form name="form1" method="post">
  <table>
    <tr><td>Username </td><td><input type="text" name="text1"></td></tr>
    <tr><td>Password</td><td><input type="password" name="pwd"></td></tr>
    <tr><td><input type="submit" value="SignIn" name="submit1"> </td></tr>
  </table>
</form>
</html>
<?php
session_start();
if(!isset($_SESSION['luser']))
{
  echo "Please Login again";
  echo "<a href='http://localhost/somefolder/login.php'>Click Here to Login</a>";

}
else
{

  $now = time(); // checking the time now when home page starts

  if($now > $_SESSION['expire'])
  {
    echo $now;
    ?> <br>
    <?
    echo $_SESSION['expire'];
    session_destroy();
    echo "Your session has expire ! <a href='http://127.0.0.1/session_exmpl/login.php'>Login Here</a>";
  }
  else
  { //starting this else one [else1]

    ?>


    <!-- From here all HTML Coding can be done -->


    <html>
    <br />
    <?
    echo $_SESSION['start'];
    ?>
    <br />
    <?
    echo $_SESSION['expire'];
    ?>
    Welcome <?php echo $_SESSION['luser'];

    ?>
    </html>

  <?php
  }
}
?>


here there are two file login.php and home.php when user successfully logedin the session start with that logedin time and session timed out after 5 minutes.

用户名
密码
home.php文件

<?php
session_start();
?>
<?php
if($_POST['submit1'])
{
  $v1 = "FirstUser";
  $v2 = "MyPassword";
  $v3 = $_POST['text1'];
  $v4 = $_POST['pwd'];
  if($v1 == $v3 && $v2 == $v4)
  {
    $_SESSION['luser'] = $v1;
    //-----------------------------------------------

    $_SESSION['start'] = time(); // taking now logged in time
    $_SESSION['expire'] = $_SESSION['start'] + (5 * 60) ; // ending a session in 30     minutes from the starting time
    header("location: http://127.0.0.1/session_exmpl/home.php");

    //-----------------------------------------------
  }
  else
  {
    echo "Please enter Username or Passwod again !";
  }

}
?>
<html>
<form name="form1" method="post">
  <table>
    <tr><td>Username </td><td><input type="text" name="text1"></td></tr>
    <tr><td>Password</td><td><input type="password" name="pwd"></td></tr>
    <tr><td><input type="submit" value="SignIn" name="submit1"> </td></tr>
  </table>
</form>
</html>
<?php
session_start();
if(!isset($_SESSION['luser']))
{
  echo "Please Login again";
  echo "<a href='http://localhost/somefolder/login.php'>Click Here to Login</a>";

}
else
{

  $now = time(); // checking the time now when home page starts

  if($now > $_SESSION['expire'])
  {
    echo $now;
    ?> <br>
    <?
    echo $_SESSION['expire'];
    session_destroy();
    echo "Your session has expire ! <a href='http://127.0.0.1/session_exmpl/login.php'>Login Here</a>";
  }
  else
  { //starting this else one [else1]

    ?>


    <!-- From here all HTML Coding can be done -->


    <html>
    <br />
    <?
    echo $_SESSION['start'];
    ?>
    <br />
    <?
    echo $_SESSION['expire'];
    ?>
    Welcome <?php echo $_SESSION['luser'];

    ?>
    </html>

  <?php
  }
}
?>


here there are two file login.php and home.php when user successfully logedin the session start with that logedin time and session timed out after 5 minutes.



欢迎 这里有两个文件login.php和home.php,当用户成功登录会话时,使用该登录时间启动,会话在5分钟后超时。
它从登录时间开始计算时间,但现在我想用空闲会话超时来更改此会话。
当用户对主页不执行任何操作时,应将其销毁会话应将其销毁,我需要对此进行更改。感谢高级…

一旦页面呈现,PHP就完成了。要做到这一点,您需要客户端语言

可能最合理的方法是使用PHP将expire值导出到JavaScript变量中,然后设置一个间隔侦听器,该侦听器将检查过期时间是否小于现在,如果小于现在,则向用户发出警报

如果您仍然想使用PHP来实现这一点(破坏会话、与数据库通信等),唯一的方法是进行AJAX调用,您仍然可以通过JavaScript进行调用。用外行的话说,PHP一呈现页面就睡着了,您可以使用AJAX调用来唤醒它

既然你没有试图解决这个问题,我就不给你任何代码了。但是,我上面写的已经足够让你开始了