naeemqaswar
10/3/2017 - 6:54 AM

Flash Messages

PHP based notification "Flash Message", implemented with the help of session. It can be used to show success and error messages in all kind of php based projects

if (!session_id()) {
	session_start();
}

function showFlashMsg(){
	$session = $_SESSION;
	$msg_html = '';

	if($session){
		if(isset($session['flash_msg'])){
			$msg_type = $session['flash_msg']['type'];
			$msg_text = $session['flash_msg']['text'];
			if($msg_type == 'error'){
				$msg_html = "<div class='alert alert-danger'>$msg_text</div>";
			} else if($msg_type == 'warning'){
				$msg_html = "<div class='alert alert-warning'>$msg_text</div>";
			} else if($msg_type == 'success'){
				$msg_html = "<div class='alert alert-success'>$msg_text</div>";
			}
		}
	}

	if($msg_html){
		unset($_SESSION['flash_msg']);
	}

	echo $msg_html;
}

function setFlashMsg($type,$msg){

	$_SESSION['flash_msg']['type'] = $type;
	$_SESSION['flash_msg']['text'] = $msg;

	return $_SESSION['flash_msg'];
}