php中不起作用的变量和会话变量包括


Variables and session variables not working in php includes

好的。。原谅我,有几个类似的问题已经发布了正确的回答。。。我知道有一天我会向互联网上的人寻求帮助,但我从未想过这会是如此愚蠢。。。

这是我的密码。。目前为止

index.php:

include "http://www.mywebsite.com/shared.php";
$page = "homepage";
include "http://www.mywebsite.com/htmlheader.php"; //trying to use "page" variable in here
echo ("<br>test2: " . $_SESSION['test']);
include "http://www.mywebsite.com/htmlfooter.php";

shared.php:

session_start();
$_SESSION['test'] = "what the f_ck im scared";

htmlheader.php:

echo ("test1: " . $page . "<br>" . $_SESSION['test']);

目前的输出是:

测试1:

测试2:

(所以页面被包括在内。只是不能使用变量。)根据我目前的理解,这应该是打印类似于:

测试1:我害怕

测试2:主页我害怕

有趣的是,我对include使用变量没有任何问题。我添加了一些东西,但后来它随机停止了工作,所以我把它简化为这个,试图找出问题所在。。我想我犯了一个愚蠢的错误。

确保已使用session_start(); 启动会话

还要检查php.ini中allow_url_fopenallow_url_include或两者是否都已设置为0(已禁用)。如果是,请尝试激活它

include "htmlheader.php";

而不是

include "http://www.mywebsite.com/htmlheader.php";

确保session_start();位于包含includes 的页面顶部