PHP中的$ _SESSION:页面重新加载时未读取变量

时间:2019-02-18 12:09:30

标签: php session

设置会话变量时,我认为它未被读取:

<?php
if(isset($_SESSION['last_activity'])){
if( $_SESSION['last_activity'] < time()-$_SESSION['expire_time'] ) { 
   //...
}else{ 
   session_destroy();
   session_start();    
   $_SESSION['last_activity'] = time(); // setting variable
   $_SESSION['expire_time'] = 8*60*60;  // calculate exp.time
   echo "branch 1";
}
}else{  
   session_start();
   $_SESSION['last_activity'] = time(); // setting variable
   $_SESSION['expire_time'] = 8*60*60; // calculate exp.time
   echo "branch 2";
}
?>

在页面重新加载时,它始终被视为“分支2”。 如果我把'session_start();'在页面顶部,每次重新加载都会从分支1.1跳转到1.2:

3 个答案:

答案 0 :(得分:1)

如果有条件,则需要在第一个会话之前开始会话。因为在检查Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 'Removing old conditional formatting if it exists already Conditions = ActiveSheet.Cells.FormatConditions.Count For i = 1 to Conditions If ActiveSheet.Cells.FormatConditions(i).Type = 2 Then If ActiveSheet.Cells.FormatConditions(i).Formula1 = "=1" Then ActiveSheet.Cells.FormatConditions(i).Delete End If Next i 'Adding new conditional formatting rule to the edited range Target.EntireRow.FormatConditions.Add Type:=xlExpression, Formula1:="=1" Target.EntireRow.FormatConditions(1).Interior.ColorIndex = 6 End Sub 是否存在之前不启动会话

SESSION

更新:

  

足以在条件出现之前开始会话,并在条件进入其他条件时破坏它

答案 1 :(得分:0)

尝试一下,它工作正常。

<?php
session_start();
if (isset($_SESSION['last_activity'])) {
    if ($_SESSION['last_activity'] < time() - $_SESSION['expire_time']) {

    } else {
        session_destroy();
        session_start();
        $_SESSION['last_activity'] = time();
        $_SESSION['expire_time'] = 8 * 60 * 60;
        echo "branch 1";
    }
} else {

    $_SESSION['last_activity'] = time(); // setting variable
    $_SESSION['expire_time'] = 8 * 60 * 60;
    echo "branch 2";
}

答案 2 :(得分:0)

添加一个细节:现在,对于每次重新加载,它都从分支1.2跳转到分支2 ...可能取决于“ session_destroy();”。 ??? 在那种情况下,我不知道会话过期后如何重设。