【问题标题】:Session based "flash" messages not displaying基于会话的“flash”消息不显示
【发布时间】:2023-04-03 12:56:01
【问题描述】:

我遇到了一个可用的会话消息系统here 没有显示消息的问题。经过大量的试验和错误后,我决定将我遇到的问题简化为以下内容以进行测试:

<?php
ini_set('error_reporting', E_ALL);

define('script_access', true);

if (!isset($_SESSION)) {
    session_start();
}

require('../framework/classes/messages.php');

$msg = new Messages();

class Test {
    public $foo;    
    public function __construct() {
        $this->foo = new Messages();
    }
    public function create_form() {
        if (isset($_POST['submit']) == 'Submit') {
            $this->form_process();
        }
        echo '<form action="' . $_SERVER['PHP_SELF'] . '?id=1&table=about" method="post">';
        echo '<input name="submit" type="submit" id="submit" value="Submit">';
        echo '</form>';

    }
    public function form_process() {
        //$new = new Messages();
        $this->foo->add('s', 'new message from');
        header("Location: message.php?proc=true");
    }

}

if ($_GET['proc'] == true) {
    echo 'should be a message here<br>';
    echo $msg->display();
    exit;
} else {
    $test = new Test();
    $test->create_form();
}
?>

经过一番折腾,我将现在注释掉的$new = new Messages(); 添加到子例程中,然后出现消息。但是,将其注释掉它们不会出现。我不知道为什么我必须重新声明一个我已经在构造函数中声明的类。有谁知道为什么会发生这种情况以及如何做到这一点,以便我只需要在构造函数中而不是在子例程中启动类?

【问题讨论】:

    标签: php class session message


    【解决方案1】:

    嗯,好吧,问题是在测试类中启动的类消息只是“生活”在那里:

    <?php
    ini_set('error_reporting', E_ALL);
    
    define('script_access', true);
    
    if (!isset($_SESSION)) {
        session_start();
    }
    
    require('../framework/classes/messages.php');
    
    $msg = new Messages();
    
    class Test {
        public $foo;    
        public function __construct() {
            $this->foo = new Messages();
        }
        public function getmessage() {
            return $this->foo->display();
        }
        public function create_form() {
            if (isset($_POST['submit']) == 'Submit') {
                $this->form_process();
            }
            echo '<form action="' . $_SERVER['PHP_SELF'] . '?id=1&table=about" method="post">';
            echo '<input name="submit" type="submit" id="submit" value="Submit">';
            echo '</form>';
    
        }
        public function form_process() {
            $this->foo->add('s', 'new message from');
            header("Location: message.php?proc=true");
        }
    
    }
    
    $test = new Test();
    
    if ($_GET['proc'] == true) {
        echo 'should be a message here<br>';
        echo $test->getmessage();
        exit;
    } else {
    
        $test->create_form();
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2014-10-11
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      相关资源
      最近更新 更多