【问题标题】:How do I access $_SESSION in an included file?如何访问包含文件中的 $_SESSION?
【发布时间】:2013-02-04 14:33:17
【问题描述】:

这是我的代码:

我的根目录是:root

位于根目录/index.php 的 index.php 文件

<?php 
require_once('root/includes/initialize.php');
<?php template('header.php', 'TITLE');?>;
?>

<div id="main">
//SOME CONTENT
</div>

我的 initialize.php 文件获取了我所有的核心包含文件并将它们放入一个“require_once”中。位于根目录/includes/initialize.php

<?php
//Define Path
defined('LIB_PATH') ? null : define('LIB_PATH', 'root/includes/');

//Core Functions
require_once(LIB_PATH.'functions.php');

//Core Objects
require_once(LIB_PATH.'_database.php');
require_once(LIB_PATH.'_session.php');

//Classes
require_once(LIB_PATH.'_user.php');
?>

更新**

我的functions.php 文件包括一个简单的模板函数,它可以获取一个模板文件,例如我的header.php。它位于根目录/includes/functions.php

<?php
//Templating
function template($path="", $pageTitle=NULL) {
    if ($pageTitle != NULL) {
        $_POST['page_title'] = $pageTitle;
    }
    include(root/public/templates/'.$path);
}
?>

我的 _session.php 文件负责我的会话控制。位于根目录/includes/_session.php

<?php

/**
* Class for Sessions
*/
class Session
{
    public $logged_in = FALSE;
    public $uid;

    function __construct() {
        session_start();
        $this->check_login();
    }

    public function check_login() {
        if (isset($_SESSION['uid'])) {
            $this->uid = $_SESSION['uid'];
            $this->logged_in = TRUE;
        } else {
            unset($this->uid);
            $this->logged_in = FALSE;
        }
    }

    public function logged_in() {
        return $this->logged_in;
    }

    public function login($user) {
        if ($user) {
            $this->uid = $_SESSION['uid'] = $user;
            $this->logged_in = TRUE;
        }
    }

    public function logout() {
        unset($_SESSION['uid']);
        session_unset();
        session_destroy();
        redirect(WEB_ROOT);
    }
}

$session = new Session();

?>

更新**

我的 header.php 位于我网站中所有页面的顶部。位于 root/public/templates/header.php。这是我遇到问题的文件,我无法弄清楚为什么我无法在此文件中回显 $session->uid 或 $_SESSION['uid']。

<html>
<head>
    <!--CSS-->
    <link rel="stylesheet" type="text/css" href="root/public/css/style.css">

    <title>MY SITE</title>
</head>
<body>
    <div id="header">
        <div id="logo">
            <a href="root"><?php echo $_POST['page_title'];?></a>
        </div>
        <?php echo $session->uid;?> //DOESN'T WORK
    </div>

我可以在我的 index.php 文件和我网站上的其他文件中很好地回显所有内容,但不能在包含的 header.php 中回显。有谁知道为什么?谢谢。

【问题讨论】:

  • session_start(); 怎么样?
  • @dar - 它在会话类构造函数中。
  • @oob - 你确定uid 属性/键实际上有一个值吗?你可以回显一个空字符串吗?
  • 尝试在 header.php 文件的顶部添加&lt;?php session_start(); ?&gt;。我知道它包含在 _session.php 文件中,但只是在这里进行故障排除。
  • 也可以试试print_r($_SESSION);

标签: php session include


【解决方案1】:

session_start() 必须在每个要设置或获取会话变量的 php 文件的开头调用。我看到你打电话给session_start() 的唯一地方是在一个文件中。

http://www.php.net/manual/en/session.examples.basic.php

<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?> 

另外附注。我正在查看您的课程Session,但我在任何地方都没有看到任何$mySession = new Session(); 也可以开始课程。

更新:

我在我的 IDE 中重新创建了您的基本文件结构和代码,并通过在类中添加这一行使其工作。

public function check_login() {
    if (isset($_SESSION['uid'])) {
        $this->uid = $_SESSION['uid'];
        $this->logged_in = TRUE;
    }
    else {
        unset($this->uid);
        $this->logged_in = FALSE;
        $_SESSION['uid'] = session_id();

        /*Add this next line */
        $this->uid = $_SESSION['uid'];
    }
}

我第一次运行 index.php 时,只是标头的 &lt;?php echo $_SESSION['uid']; ?&gt; 部分起作用。刷新和&lt;?php echo $session-&gt;uid; ?&gt; 也有效,所以它重复了两次。这告诉我你的班级没有将 ID 分配给班级变量,希望这是我想要的结果,或者你可以根据需要调整它。

更新 2:

函数文件(编辑以匹配您的路径,但您需要返回一个字符串)

<?php

//Templating
function template($path = "", $pageTitle = NULL) {
    if ($pageTitle != NULL) {
        $_POST['page_title'] = $pageTitle;
    }
    return "$path";
}
?>

然后在 Index.php 文件中添加这种方式:

<?php
require_once('initialize.php');
include(template('header.php', 'TITLE'));
//include('header.php');
?>

<div id="main">
    //SOME CONTENT
</div>
</body>
</html>

_session.php 文件:

<?php

/**
 * Class for Sessions
 */
class Session
{

    public $logged_in = FALSE;
    public $uid;

    function __construct() {
        session_start();
        $this->check_login();
    }

    public function check_login() {
        if (isset($_SESSION['uid'])) {
            $this->uid = $_SESSION['uid'];
            $this->logged_in = TRUE;
        }
        else {
            unset($this->uid);
            $this->logged_in = FALSE;

            $_SESSION['uid'] = session_id();
            $this->uid = $_SESSION['uid'];
        }
    }

    public function logged_in() {
        return $this->logged_in;
    }

    public function login($user) {
        if ($user) {
            $this->uid = $_SESSION['uid'] = $user;
            $this->logged_in = TRUE;
        }
    }

    public function logout() {
        unset($_SESSION['uid']);
        session_unset();
        session_destroy();
        redirect(WEB_ROOT);
    }

}

$session = new Session();
?>

还有header.php

<html>
    <head>
        <!--CSS-->
        <link rel="stylesheet" type="text/css" href="style.css">

        <title>MY SITE</title>
    </head>
    <body>
        <div id="header">
            <div id="logo">
                <a href="root"><?php echo $_POST['page_title'];?></a>
            </div>
            <?php echo $session->uid; ?> //WORKS NOW
            <?php echo $_SESSION['uid']; ?> //WORKS NOW
        </div>

【讨论】:

  • Session在Session类结束时实例化($session = new Session),在Session的__construct方法中调用session_start,所以每次调用$session时都会调用。
  • 考虑到如果我加载我的 index.php 它包括initial.php 然后是header.php,它不应该工作吗?我尝试在 header.php 中包含 _session.php 但我遇到了同样的问题:(
  • 好的,所以我尝试在 header.php 的顶部只做一个include('include/_session.php'),但是当我尝试加载 index.php 时出现此错误Fatal error: Cannot redeclare class Session in root/includes/_session.php on line 6
  • 感谢您与我一起解决这个问题。 $_SEESION['uid'] 由 _session.php 文件中的 login($user) 方法设置。当用户登录时,他们的用户名和密码被验证,然后他们的 id 被传递给方法中的 $user 变量。我可以通过回显 $_SESSION['uid'] 来获取 header.php 文件中的 $_SESSION['uid']。我现在遇到的问题是访问 header.php 中的 $session 对象。
  • ok.. 所以现在我感觉很糟糕:(。抱歉。我试图让这个问题更具可读性,但在这样做时我忘了添加我的部分代码。我得到了 header.php通过functions.php文件中的一个函数文件。当我编辑它时,它只是通过执行include('root/public/templates/header.php)来绕过该函数,但当我使用template()函数时却不行。我更新了上面的所有内容。再次抱歉。跨度>
【解决方案2】:

这个问题很模糊。我的猜测是,既然你的index.php 文件位于root/index.php,那么你的包含路径:

require_once('root/includes/initialize.php');
include('root/public/templates/header.php');

不正确。您不以/ 开头,因此路径是相对的,并且考虑到您的index.php 的位置,您将包括root/root/includes/initialize.php。如果是这种情况,您应该很容易通过页面上缺少&lt;title&gt;MY SITE&lt;/title&gt;&lt;a href="root"&gt;TITLE&lt;/a&gt; 来发现这一点。没有吗?

如果是这个问题,我建议你定义某种HOME 常量,例如

define ('HOME', dirname(__FILE__));
// or define ('HOME', __DIR__); depending on your PHP version

这样您就可以包含与该常量相关的所有内容

require_once(HOME . '/includes/initialize.php');

除此之外,我在您的代码中看不到任何错误。

【讨论】:

  • 对不起,我不清楚。我在这个问题中使用 root 作为我网站的 ROOT 目录的占位符。我在构建网站时处于本地环境中,所以我的根目录类似于 /Users/myname/etc。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多