Php 类属性的内容在输出到浏览器之前被清除
我在一个名为Php 类属性的内容在输出到浏览器之前被清除,php,class,Php,Class,我在一个名为site的类中有这个,还有其他无害的东西: private $notice_type = '', $notice_msg = ''; public function setNotice($type,$msg){ $this->notice_type=$type; $this->notice_msg=$msg; } public function notice($what){ switch($what){
site
的类中有这个,还有其他无害的东西:
private
$notice_type = '',
$notice_msg = '';
public function setNotice($type,$msg){
$this->notice_type=$type;
$this->notice_msg=$msg;
}
public function notice($what){
switch($what){
case 'type': return $this->notice_type; break;
case 'msg': return $this->notice_msg; break;
}
}
public function clearNotice(){
$this->notice_type='';
$this->notice_msg='';
}
我已将该类设置为如下会话:
$\u会话['site']=新站点()代码>
下面是我如何使用它的一个场景:
提交表格后;我这样设置通知:$\u SESSION['site']->setNotice('success','success message')代码>,如果是这种情况,则返回错误,并使用header()
将用户重定向到此处
然后我在登录页上输出如下消息:
echo$_会话['site']->通知('msg')代码>
$\u会话['site']->clearNotice()代码>
但是;当我使用clearNotice()
-函数时,$notice\u type
和$notice\u msg
的内容在输出到浏览器之前被清除
我需要让它一直呆到用户以某种方式离开页面为止。我错过了什么?我不知道发生了什么。但不知怎的,这个脚本开始按预期工作。
我已经一次又一次地重写了代码,据我所知,它和以前差不多。但无论如何;这就是现在的效果:
site()-类:
该类控制通知以及由用户设置的设置,如数据的首选排序方向和值得记住的选择,以获得更好的用户体验等
我已经创建了一个模板,在输出页面内容之前,我在其中有以下代码:
基本上,它只是检查是否有任何消息显示
然后我加载页面内容,最后我有这样一个:
在用户关闭通知或离开页面之前,通知应一直可见。我不想,也不需要,保留这个信息
现在;每当我需要向用户提供有关其操作的反馈时(例如,在成功提交表单后),我可以在脚本末尾这样做:
$_SESSION['site']->newNotice('success','<b>Success!</b> Your request was submitted successfully...');
header('Location '.$_SERVER['HTTP_REFERER']);
exit;
$\u会话['site']->newNotice('success','success!您的请求已成功提交…);
标题('Location'.$\u SERVER['HTTP\u REFERER']);
出口
它就像一个符咒…session\u start()在页面顶部session\u start()
在页面顶部。。。当我不使用clearNotice()
-函数时,脚本可以工作。消息得到了输出,但我永远留在那里——有点……认为你必须在site类上实现u sleep,才能序列化私有属性,我不明白。最好找到另一种方法来解决这个问题。。。我需要有一个简单的方法来设置和检索执行后的消息…您正在回显$\u SESSION['site']->注意('msg')代码>对吗?
<?php
# check to see if session is started
if(!isset($_SESSION)){session_start();}
//
// check if site()-class should be activated for this site
if($_SITE_CLASS_site=='Yes'){
# if Yes; prevent resetting the class if it has already been started.
if(!isset($_SESSION['site'])){$_SESSION['site']=new site();}
//
}
//
?>
<?php if ($_SITE_CLASS_site=='Yes'&&$_SESSION['site']->notice('msg')!=''): ?>
<div id="site-notice-<?=$_SESSION['site']->notice('type')?>" class="grid_12"><p><?=$_SESSION['site']->notice('msg')?></p></div>
<?php endif; ?>
<?php
if ($_SITE_CLASS_site=='Yes'&&$_SESSION['site']->notice('msg')!=''):
$_SESSION['site']->clearNotice();
endif;
?>
$_SESSION['site']->newNotice('success','<b>Success!</b> Your request was submitted successfully...');
header('Location '.$_SERVER['HTTP_REFERER']);
exit;