繁体   English   中英

PHP会话变量在刷新/重定向后丢失

[英]PHP Session variable lost after refresh / redirect

我正在制作登录页面,并且一切正常,但是在刷新安全页面或重定向到另一个页面变量时消失了。

登录:

<?php session_start(); 

//connection to database and other stuff

if ($user == $dbuser && $pass == $dbpass) {

    $_SESSION['user'] = $user;
    $_SESSION['authenticated'] = "yes";
    $suser = $_SESSION['user']; //just to test if session works

}

?>

安全页面:

<?php session_start(); ?>

//small amount of html here

<?php
    $suser = $_SESSION['user'];
    $sauth = $_SESSION['authenticated'];

    if ($sauth != 'yes') { //not completed yet (needs user name check)
        echo "<a href='./'>Log in</a> first!";
        require ('./login.php');
        die;
    } else {
    //not so important code here
    }
?>

//rest of html here

我没有注意到任何错误,并且错误日志文件是干净的,因此它一定是其他东西。 重定向到安全页面后,会话工作正常,但是正如我之前所说,刷新页面或其他重定向会清除会话变量。

页面: http : //nano.filiparag.com/admin/如果要测试,只需输入用户名和密码

注意:
现在,我尝试使用setcookie()执行此操作,但此操作不再起作用


解决了

问题出在注销按钮上。 我为此制作了单独的PHP文件,现在一切正常。

我想你可以尝试使用

登录:

<?php session_start(); 
ob_start();
//connection to database and other stuff

if ($user == $dbuser && $pass == $dbpass) {

    $_SESSION['user'] = $user;
    $_SESSION['authenticated'] = "yes";
    $suser = $_SESSION['user']; //just to test if session works

}

?>

安全页面:

<?php session_start(); 
  ob_start();?>

//small amount of html here
<?php session_start(); ob_start();
    $suser = $_SESSION['user'];
    $sauth = $_SESSION['authenticated'];

    if ($sauth != 'yes') { //not completed yet (needs user name check)
        echo "<a href='./'>Log in</a> first!";
        require ('./login.php');
        die;
    } else {
    //not so important code here
    }
?>

//rest of html here

我想您的login.php脚本在包含会话变量时会有所作为。 我可以看看您的login.php脚本,然后编辑答案

暂无
暂无

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

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