未设置时的php会话变量问题

时间:2011-09-21 09:45:46

标签: php session-variables unset

我已经设置了一个函数来设置会话变量$ _SESSION ['flash']以便在页面之间存储消息

function setFlash($string,$type="info") {

    switch ($type) {
        case "warning":
            $_SESSION['flashtype'] = "warning";
            break;
        case "error":
            $_SESSION['flashtype'] = "error";
            break;
        case "info":
            $_SESSION['flashtype'] = "info";
            break;
        default:
            $_SESSION['flashtype'] = "info";
            break;
    }
    $_SESSION['flash'] = $string;   
}

以及打印此消息的功能

function printFlash() {
      echo $_SESSION['flash'];
      unset($_SESSION['flash']);

}

我在每个页面的顶部调用此函数(自然在session_start之后)

问题是它什么都不打印,但如果我评论“unset($ _ SESSION ['flash']);”它在每个页面中打印消息。

我该如何解决?


对不起我的错。

my page is something like this

include "func.inc.php"
session start

function editSomething {
  that call setFlash()
  }

include "template.php" (where printFlash() is called)

现在我把printFlash直接放在我的页面上并且工作......很奇怪......我的错误是什么?

1 个答案:

答案 0 :(得分:0)

在每一页上都是这样:

  • 进行会话
  • 显示闪光灯
  • 删除闪光灯
  • 使用值
  • 创建'flash'

您必须在显示之前移动创建。

(它也不是很有用,因为你不发送'flash'(它在创建后立即被删除)