Simple Session-Based Flash Message
Set the Session just in case
//Ensure that a session exists (just in case) if( !session_id() ) { session_start(); }
The rest is as easy as it gets!
The Function.
/** * Function to create and display error and success messages * @access public * @param string session name * @param string message * @param string display class * @return string message */ function flash( $name = '', $message = '', $class = 'success fadeout-message' ) { //We can only do something if the name isn't empty if( !empty( $name ) ) { //No message, create it if( !empty( $message ) && empty( $_SESSION[$name] ) ) { if( !empty( $_SESSION[$name] ) ) { unset( $_SESSION[$name] ); } if( !empty( $_SESSION[$name.'_class'] ) ) { unset( $_SESSION[$name.'_class'] ); } $_SESSION[$name] = $message; $_SESSION[$name.'_class'] = $class; } //Message exists, display it elseif( !empty( $_SESSION[$name] ) && empty( $message ) ) { $class = !empty( $_SESSION[$name.'_class'] ) ? $_SESSION[$name.'_class'] : 'success'; echo '<div class="'.$class.'" id="msg-flash">'.$_SESSION[$name].'</div>'; unset($_SESSION[$name]); unset($_SESSION[$name.'_class']); } } }
Setting Messages
//Set the first flash message with default class flash( 'example_message', 'This content will show up on example2.php' ); //Set the second flash with an error class flash( 'example_class', 'This content will show up on example2.php with the error class', 'error' );
Displaying the Messages
<?php flash( 'example_message' ); ?> <?php flash( 'example_class' ); ?>
Leave a Reply