简体   繁体   English

为什么php页面刷新发布会话变量

[英]Why does a php page refresh release session variable

I am trying to release/unset a session variable after 2 minutes through unsetsession.php : 我试图通过unsetsession.php 2分钟后释放/取消设置会话变量:

<?php

    session_start();

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 120)) {
    // last request was more than 2 minutes ago
    unset($_SESSION['logged_in']);     // unset $_SESSION variable for the run-time
    unset($_SESSION['LAST_ACTIVITY']);
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

?>

where the session variables are: 会话变量在哪里:

$_SESSION['logged_in'] = 1;
$_SESSION['LAST_ACTIVITY'] = time();

However, the session variables do not get unset but are unset when I refresh the page. 但是,会话变量不会被重置,而是在刷新页面时被重置。 The page is making a call to the file unsetsession.php. 该页面正在调用文件unsetsession.php。 Why is the session not getting unset after 2 minutes? 为什么2分钟后会话仍未取消?

EDIT : The page file.php has <?php include("includes/unsetsession.php") ?> on top of the page. EDIT :页面file.php在页面顶部具有<?php include("includes/unsetsession.php") ?> When file.php is refreshed on the browser, the value of the session variable $_SESSION['logged_in'] = 1; 在浏览器上刷新file.php时,会话变量$_SESSION['logged_in'] = 1; gets changed to 0. It would not change after 2 mins. 被更改为0。2分钟后将不会更改。 It only changes to 0 when the page is refreshed. 仅在刷新页面时它才变为0。

I am checking the value by running getsession.php on the browser which is: 我通过在浏览器上运行getsession.php来检查值:

<?php
  session_start();
  if(isset($_SESSION['logged_in']))
  echo "1";
  else
    echo "0";
?>

Well, you clearly have a problem on understanding how php works. 好吧,您显然在理解php的工作方式上存在问题。 Whenever you load your page, your php script will be executed once . 每当您加载页面时,您的php脚本就会执行一次

So you're going through your code the first time, your session variable is set. 因此,您是第一次遍历代码,因此已设置了会话变量。 And now, it's over . 现在结束了 Your php script has executed and it's just finished, it won't do anything anymore. 您的php脚本已执行并且刚刚完成,将不再执行任何操作。

But if you refresh your page, the script will be called again. 但是,如果刷新页面,该脚本将再次被调用。 That time, the session variable is already set, and let's say it has been two minutes. 到那时,会话变量已经设置好了,假设已经两分钟了。 So you enter the condition and destroy it. 因此,您输入条件并销毁它。 And your second call to the script is finished. 至此,您对脚本的第二次调用完成了。


So there's no really way on finding a solution to your problem right now, before you give us any more information on what you're trying to achieve. 因此,在向我们提供有关您要实现的目标的更多信息之前,目前还没有真正的方法可以找到解决问题的方法。

Some solutions would be using javascript to regularly make an AJAX request to unsetsession.php for instance, or simply letting this as is: the session will not be unset until you load a page. 例如,某些解决方案将使用javascript定期向unsetsession.php发出AJAX请求,或者只是按原样进行此操作:在加载页面之前,不会取消设置会话。 That's not really a problem, most websites do it that way. 这并不是真正的问题,大多数网站都是这样做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM