简体   繁体   English

取消设置时的PHP会话变量问题

[英]php session variable problem when unset

i have made a function to set a session variable $_SESSION['flash'] in order to store a message between page 我做了一个函数来设置会话变量$ _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;   
}

and a function to print this message 和打印此消息的功能

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

}

i call this function at the top of every page (naturally after session_start) 我在每个页面的顶部调用此函数(自然在session_start之后)

the problem is that it doesn't print nothing, but if I comment " unset($_SESSION['flash']);" 问题是它什么也不打印,但是如果我评论“ unset($ _ SESSION ['flash']);“, it prints the message in every page. 它在每页中打印消息。

how can i solve? 我该如何解决?


Solved sorry my fault. 解决了对不起我的错。

my page is something like this

include "func.inc.php"
session start

function editSomething {
  that call setFlash()
  }

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

now i put printFlash directly in my page and works..bah strange...what's my mistake? 现在我将printFlash直接放在页面中并且可以工作..bah奇怪...我的错误是什么?

On every page this is what happened: 在每一页上都是这样:

  • Make a session 进行会议
  • Display flash 显示闪光灯
  • Delete flash 删除闪光灯
  • Create 'flash' with value 创造价值的“闪光”

You have to move Create before display. 您必须在显示之前移动创建。

(it's also not very usefull because you do not transmit 'flash' (it's delete right after been created) (它也不是很有用,因为您不会传输“闪存”(在创建后立即删除)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM