无法在包含的.php文件中获取会话值

时间:2016-10-29 07:18:47

标签: php session

我试图获得会话值$_SESSION['LinkID'],但那里什么也没有。

在main.php中

<?php
session_start();
if(!isset($_SESSION['login_user'])){
    header("location: ../index.php");
}
?>

<div id="Wrapper">
    <div id="Banner">
        <div id="Logo">
            <img src="../images/banner.png" width="200" height="60" alt=""/>
        </div>
        <div id="Navigation">
            <?php include ("../cmd/funct/createlink.php");?>
            <?php $_SESSION['LinkID'] = GenerateLinkID();?>
            <?php include ("../modules/menu/menu.php");?> <!-- $_SESSION['LinkID'] Not Empty -->
        </div>
    </div>
    <div id="oContent">
        <?php echo $_SESSION['LinkID']?> <!-- $_SESSION['LinkID'] Not Empty -->
        <?php include ("../cmd/funct/openpages.php");?> <!-- $_SESSION['LinkID'] EMPTY -->
        <?php echo "<br>".$_SESSION['LinkID']?> <!-- $_SESSION['LinkID'] Not Empty -->
    </div>
</div>

在openpages.php(包含在main.php中)

<?php
    echo $_SESSION['LinkID']; //$_SESSION['LinkID'] Not Empty
    if (empty($_GET[$_SESSION['LinkID']])) {
        echo 'EMPTY';
    } else {
        echo 'NOT EMPTY';
            $oXo = htmlspecialchars($_GET[$_SESSION['LinkID']]);
            if ($oXo==EncryptLinkKey('encrypt', 'home')){
                include ("statistic.php");
            } elseif ($oXo==EncryptLinkKey('encrypt', 'sin')) {
                include ("mailing/in/index.php");
            } elseif ($oXo==EncryptLinkKey('encrypt', 'sout')) {
                include ("mailing/out/index.php");
            } elseif ($oXo==EncryptLinkKey('encrypt', 'srep')) {
                include ("mailing/report/index.php");
            } elseif ($oXo==EncryptLinkKey('encrypt', 'slogout')) {
                include ("../logout.php");
                header("location: ../index.php");
            }   
    }
?>

如果我在openpages.php中添加session_start(),它会显示通知

  

会话已经开始 - 忽略session_start()

并且结果始终为“EMPTY”

openpages.php中有什么问题...我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

在两个文件的最顶部,尝试建立一个这样的开放会话:

<?php
    // FIRST CHECK IF SESSION EXIST BEFORE STARTING IT.
    // DO THIS AT THE VERY TOP OF EACH SCRIPT THAT NEEDS ACCESS TO $_SESSION DATA
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

之后,您可以开始添加与$_SESSION

相关的代码

答案 1 :(得分:0)

也许你的函数什么都不返回?

<?php $_SESSION['LinkID'] = GenerateLinkID();?>

请在$_SESSION['LinkID']main.php内写下以下内容:

<?php $_SESSION['LinkID'] = GenerateLinkID();
echo $_SESSION['LinkID'];?>