Flash Messages don't disappear PHP

368 Views Asked by At

I'm using this Flash Messages Script for a simple redirect and flash message system. Everything works fine on my apache localhost, but as soon as I upload it to a server (also apache) it doesn't work. It sets the sessions and also displays the messages correctly, but it doesn't unset the messages afterwards. Now I have a whole bunch of "Flash messages" on my website and they'll get more and more unless you close your browser to unset all sessions forcefully.

I've already read the documentation like a thousand times and also searched in the Flash Messages script on the server for any errors. I couldn't find any.

Maybe you guys can help me. The host where I'll deploy my website is strato.com.

Edit: I found a cookie called PHPSESSID in my browser informations. Maybe this could be helpfull.

Constructor:

    public function __construct()
{

    // Generate a unique ID for this user and session
    $this->msgId = sha1(uniqid());

    // Create session array to hold our messages if it doesn't already exist
    if (!array_key_exists('flash_messages', $_SESSION)) $_SESSION['flash_messages'] = [];

}

Clear session function:

    protected function clear($types=[])
{
    if ((is_array($types) && empty($types)) || is_null($types) || !$types) {
        unset($_SESSION['flash_messages']);
    } elseif (!is_array($types)) {
        $types = [$types];
    }

    foreach ($types as $type) {
        unset($_SESSION['flash_messages'][$type]);
    }

    return $this;
}

Add Sessions:

    public function add($message, $type=self::defaultType, $redirectUrl=null, $sticky=false)
{

    // Make sure a message and valid type was passed
    if (!isset($message[0])) return false;
    if (strlen(trim($type)) > 1) $type = strtolower($type[0]);
    if (!array_key_exists($type, $this->msgTypes)) $type = $this->defaultType;

    // Add the message to the session data
    if (!array_key_exists( $type, $_SESSION['flash_messages'] )) $_SESSION['flash_messages'][$type] = array();
    $_SESSION['flash_messages'][$type][] = ['sticky' => $sticky, 'message' => $message];

    // Handle the redirect if needed
    if (!is_null($redirectUrl)) $this->redirectUrl = $redirectUrl;
    $this->doRedirect();

    return $this;
}
1

There are 1 best solutions below

1
On

I fixed it. It was due an change in PHP 7.1 in the php.ini file. As soon as I downgraded my PHP version to PHP 7.0 everything worked fine again.

I hope this will help a lot of people. At least you've got some starting point now.