PHP flash message using session
Flash message is used to convey success messages, error messages, or other important information from one page into another page. By using $_SESSION
we can transfer a message from one page to another. This examples shows how to display session based flash message in PHP.
flash.php
<?php function flash($name='',$msg='',$cate='green'){ if(!empty($name)){ if(!empty($msg)&&empty($_SESSION[$name])){ $_SESSION[$name]=$name; $_SESSION[$name."_msg"]=$msg; $_SESSION[$name."_cate"]=$cate; } else if(empty($msg)&&!empty($_SESSION[$name])){ echo "<h3 style='padding:5px;color:white;background-color:{$_SESSION[$name."_cate"]}' >{$_SESSION[$name."_msg"]}</div>"; unset($_SESSION[$name]); unset($_SESSION[$name."_msg"]); unset($_SESSION[$name."_cate"]); } } } ?>
login.php
<?php session_start(); include "flash.php"; #Call flash for assign message in login Page flash('login','Login Successfully'); //header("location:home.php"); ?>
home.php
<?php session_start(); include "flash.php"; #call flash for display message echo flash('login'); ?>