【问题标题】:PHP Session Variables doesn't workPHP 会话变量不起作用
【发布时间】:2014-05-16 01:38:03
【问题描述】:

我的网站有一个登录页面,其中显示了世界地图,其中包含每个大陆的悬停链接。点击它会有一个像这样的 HREF 链接:

<a href="setsession.php?c=AU">Australia</a>

setsession.php 只包含这个:

<?php
    session_start();

    $_SESSION['SelectedCountry'] = htmlspecialchars($_GET["c"]);

    header("location:test/index.php");
?>

并且 test/index.php 包含:

<?php
    session_start();
    echo $_SESSION['SelectedCountry'];
?>

所以我不明白为什么 test/index.php 只显示一个空白页?

【问题讨论】:

  • 也许是间距? header("Location: test/index.php");
  • 在 index.php 中尝试 echo ... , print_r($_SESSION);检查是否设置了会话 SelectedCountry
  • 它可以转发到 test/index.php。问题是 test/index.php 只显示一个空白页面,而不是回显会话变量。
  • "print_r($_SESSION);"只返回“数组()”
  • 您的浏览器是否显示有 PHPSESSID cookie?如果是这样,您能找到与该 cookie id 匹配的会话文件吗?如果是,该会话文件的内容是什么?

标签: php session variables


【解决方案1】:

已经设法解决了这个问题,并且对其他人有用。

使用会话变量时,必须全部在一个域中。

由于我的域的设置,/test/index.php 在同一主机上但在不同的域上(但仍然是与 setsession.php 和我的初始登录页面相关的子文件夹)。

test/index.php 显示在与其他两个文件不同的域中。所以landing.php 转发到同域的setsession.php,setsession.php 转发到不同域的test/index.php。这就是它崩溃的地方。

所以现在 - 我已经将landing.php 转发到现在位于第二个域上的 setsession.php - 所以来自 setsession.php 和去 test/index.php 的变量在同一个域上。

问题解决了:)

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2015-09-30
    • 2012-08-17
    相关资源
    最近更新 更多