3

I'm having a bizarre problem accessing php SESSION data from an HTML page. I have a basic login.php which I redirect to 'page1.html.' Now I have configured the .htaccess as such:

AddType application/x-httpd-php5 .html .htm

Plus, I know that my php code is working on any given HTML page.

However, if I put :

session_start();

....

if(!isset($_SESSION['username'])){
    header ("Location: http://blahblahblah/Login.html");
}

inside the body of page1.html, the if statement always fires (in fact, a var_dump($_SESSION) yields "array(0) { }").

But, if I take the same code, put it in 'page1.php,' everything works as expected (and a var_dump($_SESSION) gives proper values).

So, is it not possible to access $_SESSION from an HTML page the same way you would if its extension was renamed to '.php'? Or is there something I am doing wrong here? I would rather like to keep most of my pages as '.html' as opposed to '.php,' as it would be an inconvenience to go back and change all the file extensions, etc.

Much appreciated,

4

1 回答 1

0

根据您的描述,PHP 运行正常(因为 if 语句正在触发)。

需要检查的一些事项:

  • 是否session_start()在 page1.html 中被调用?必须调用它来检索会话数据并填充$_SESSION超级全局。
  • 在发送标头之前被session_start()调用?在发送任何 HTML 或空格之前检查会话是否已启动,否则会话 cookie 将不会与标头一起发送。
  • Login.html 和 page1.html 是否在同一个域和同一个子域上? $_SESSION如果会话 cookie 不可用,则可能为空。如果您尝试跨域使用会话,则可能会发生这种情况。
  • 是否有会话 cookie,是否正在发送?在导航到 page1.html 时,使用 Firebog/Inspector 等检查以确保会话 cookie 存在,并且它正在与页面请求一起传递。
  • $_SESSION变量是如何设置的?page1.php 起作用可能只是巧合,因为$_SESSION当您第一次尝试访问 page1.html 时,该变量可能没有正确设置
  • Apache 是否有权写入session.save_pathphp.ini 中声明的内容?
  • session.save_path有效路径吗?
于 2013-01-13T06:28:31.967 回答