Skip to content

Instantly share code, notes, and snippets.

@matezito
Created February 20, 2021 19:24
Show Gist options
  • Save matezito/dc11a92fb1326a17481817deedb03773 to your computer and use it in GitHub Desktop.
Save matezito/dc11a92fb1326a17481817deedb03773 to your computer and use it in GitHub Desktop.
Flash Session
<?php
class Flash_Session {
static private $initialized = false;
public function __construct()
{
if (self::$initialized)
return false;
self::$initialized = true;
}
/***
* Set Flash messages
*/
public static function set_flash_session($class, $msg)
{
/**
* Init sessions if not
*/
if (!session_id()) {
session_start();
}
/**
* Create session if not exist
*/
if (!isset($_SESSION['flash_messages'])) {
$_SESSION['flash_messages'] = ["hola"];
}
$_SESSION['flash_messages'] = [
'name' => $class,
'msg' => $msg
];
return $_SESSION['flash_messages'];
}
/**
* Show Flash Messages
*/
public static function show_flash_session()
{
if (isset($_SESSION['flash_messages']) && !empty($_SESSION['flash_messages'])) {
echo '<div class="notice notice-' . $_SESSION['flash_messages']['name'] . ' is-dismissible">
<p>' . $_SESSION['flash_messages']['msg'] . '</p>
</div>';
}
unset($_SESSION['flash_messages']);
}
}
/**
* Set
* Flash_Session::set_flash_session('div-class','Message');
*/
/**
* Show message
* Flash_Session::show_flash_session()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment