页面刷新后,PHP会话变量消失

时间:2013-03-17 03:42:30

标签: php session variables

我有一个大问题,我无法解决它。我的会话变量在文件之间交换,但刷新第二页后它们就消失了。

以下是代码:

的index.php     

session_start();

header('Title: So random');
header('charset: UTF-8');

//if index.php?login is requested
if(isset($_REQUEST['login'])) {

    //'pass' input box value (from POST) is saved to $pass variable.
    $pass = $_POST['password'];

    //if pasword matches Password.
    if($pass == 'Password') {

        //session_start();

        $_SESSION['logintoken'] = "approoved";
        header("Location: list.php");
        die();

    } else { $error = true; }

}

if(isset($_SESSION['logintoken'])) { 
        header('Location: list.php'); 
        die();
    }


?>
Random HTML With login page goes here...

然后我们有了页面,只有登录时才可以使用。从登录重定向到它之后没关系,但刷新后我“logintoken未定义”。

list.php的

<?php
session_start();

if($_SESSION['logintoken'] != "approoved") { 
    //'<meta http-equiv="REFRESH" content="0; url=index.php">'
    die();
}

?>
<html> goes here....

1 个答案:

答案 0 :(得分:0)

header('Title: So random');
header('charset: UTF-8');   
$_SESSION['logintoken']='' ;

编辑: 或许这更好

if(isset($_SESSION['logintoken']) && ($_SESSION['logintoken'] != "approoved"))

编辑2:

    header("Location: list.php?token=".$_SESSION['logintoken']);

list.php的

if($_REQUEST['token'] != "approoved") { 
    //'<meta http-equiv="REFRESH" content="0; url=index.php">'
    die();
}