我正在創建一個使用會話存儲消息的通知類。我需要將它們創建爲多維數組,因此我可以利用不同的「名稱空間」,從而防止消息顯示在錯誤的頁面上。PHP會話,爲什麼數組設置爲1
下面是一個例子:
print_r($_SESSION)
Array
(
[EVENT_CMS] => Array
(
[Notifier] => Array
(
[0] => 'Your settings have been saved.'
[1] => 'You must re-upload...'
)
)
)
現在在設置頁面上,這些消息將會以適當的方法的調用打印。
我在設置類中的消息容器時遇到了問題。這是我的構造是什麼樣子:(該SESSION_NAMESPACE常量的定義,因此執行真正塊)
public function __construct($namespace = 'Notifier') {
$this->_session_start();
if(defined('SESSION_NAMESPACE')){
$this->notifications =& $_SESSION[SESSION_NAMESPACE][$namespace];
} else {
$this->notifications =& $_SESSION[$namespace];
}
}
$Notify = new Notifier();
$Notify->add($_GET['test']);
print_r($_SESSION);
上面的代碼產生了我這個數組:
$_SESSION
Array
(
[EVENT_CMS] => Array
(
[Notifier] => 1
)
)
添加消息方法應該更新會話,對吧?由於通知數組是一個參考?對update_session()的調用對輸出沒有影響...
public function add($message, $class = NULL) {
$message_node = $message;
$this->notifications[] = $message_node;
$this->update_session();
}
public function update_session(){
$this->SESSION[$this->namespace] &= $this->notifications;
}
你在哪裏將數組存儲到會話中? – 2012-02-13 20:16:24
該示例不完整。顯示完整的代碼,它會更容易回答。如果您在其他地方訪問會話變量,這並不明顯。 – 2012-02-13 20:18:58
@DavidChan - 我用相關代碼更新了問題;這些消息應該存儲在會話數組本身中。 – AVProgrammer 2012-02-13 20:30:46