我使用$_SESSION['siteRoot']
存储我网站的根地址(它基本上是一个框架,因此根据用于访问网站的URL,这可能会发生变化)。我需要在我的一些javascript文件中使用此值...
到目前为止,我一直将我的js文件包含为.php
,并将以下代码放在js
文件的顶部,如下所示:
<?php
header("Content-type: application/javascript");
session_start();
?>
这在我的本地主机上运行正常以进行测试 - 但是当我将其上传到实时服务器时,我注意到每次重新加载页面时我的会话都被重置,并且在调试的那一天终于发现它是session_start();我的java脚本文件中的行导致了这种行为。
我尝试了以下内容:
if (!isset($_SESSION))
{
session_start();
}
if (session_id() == '')
{
session_start();
}
if (session_status() !== PHP_SESSION_ACTIVE)
{
session_start();
}
并且完全不考虑session_start。如果我没有启动会话,那么我就不能使用这些变量(很明显......),但我找不到一种方法来启动它,而不会擦除我在主页面中创建的会话。 / p>
有什么想法吗?
答案 0 :(得分:0)
The problem was not caused by the session itself exactly, but because I hadn't included my class autoloader before calling the session, and so my custom classes were not surviving the deserialize process (even though it's a javascript file and I don't use any of said classes!)
I changed my javascript code to:
<?php
include ('include/autoloader.php');
header("Content-type: application/javascript");
session_start();
?>
and everything works fine. Easy to forget when you're not using any of the serialized classes on that page!