PHP how to handle user flash messages via session
Handle user flash messages via session variables which is more professional and easy to manage, How we can display these messages in traditional way, developers know how they display messages and update users about status of user operation(s) like
- record save successfully
- record updated successfully
- Failed to create account, please try after a while
Most of new developers manage these kind of messages by sending the whole message in get variable, or some code for message like true for success message and false for error message.
like in the following example we have to redirect from add.php to index.php while sending message from add.php and displaying message on index.php
1 2 3 4 5 6 7 8 9 10 11 |
<?php /**********************add.php**/ if($_POST){ $qry = "insert into tbl_users values(".$_POST.")"; if(mysql_query($qry)){ header('Location:index.php?msg=Account created successfully.'); }else{ header('Location:index.php?msg=ERROR: could not create account.'); }//endif query executed }//endif post ?> |
At top on index.php page right after body tag
1 2 3 4 5 6 |
<?php /**********************index.php**/ if(isset($_GET['msg'])){ echo $_GET['msg']; } ?> |
the above method of displaying messages is ok but not good and professional, we can improve our messaging system by sending the message in session variable instead of query string ($_GET variable)
that has the following advantages
- User can not alter the message
- Message can be removed after displaying, means on refresh user will not be seeing same message stick over there.
- Its professional way of doing it
How to handle user flash messages via session
In above add.php to index.php scenario, we will set a session variable $_SSESSION[‘msg’] and assign it the message after that we will redirect to index.php
at index.php we will check if $_SSESSION[‘msg’] is set if so we will display it as message and unset it.
see above code with sessions
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /**********************add.php**/ if($_POST){ $qry = "insert into tbl_users values(".$_POST.")"; if(mysql_query($qry)){ $_SESSION['msg'] = 'Account created successfully.'; }else{ $_SESSION['msg'] = 'ERROR: could not create account.'; }//endif query executed header('Location:index.php'); }//endif post ?> |
At top on index.php page right after body tag
1 2 3 4 5 6 7 |
<?php /**********************index.php**/ if(isset($_SESSION['msg'])){ echo $_SESSION['msg']; unset($_SESSION['msg']);//if user refresh index.php after 1st time it will not see the message } ?> |
it can be improved more by creating a global custom functions for it
we can create single function but here I will make it simpler and do with two functions
- set_msg() Will set the message, it has 2 parameters 1st is mandatory which is message string, second one is optional which is message type to show message in green, yellow or red
- get_msg() it has no parameter, it checks id message is set it displays and unset it, this way on page refresh as it is unset so it will not be displayed again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php /*********************global functions to be accessed in all pages**/ function set_msg($msg,$type=null){ $_SESSION['msg'] = $msg; $_SESSION['type'] = $type;//success,warning,danger }//end set_msg() function get_msg(){ if($_SESSION['msg']){ $type = isset($_SESSION['type'])?$_SESSION['type']:'success'; echo '<div class="alert alert-'.$type.'">'; echo $_SESSION['msg']; echo '</div>'; //now remove msg & type from session unset($_SESSION['msg']); unset($_SESSION['type']); }//endif isset session[msg] }//end get_msg() ?> |
now let see above add.php and index.php code with these functions
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php /**********************add.php**/ require_once('global-functions.php'); if($_POST){ $qry = "insert into tbl_users values(".$_POST.")"; if(mysql_query($qry)){ set_msg('Account created successfully.'); }else{ set_msg('ERROR: could not create account.','danger'); }//endif query executed header('Location:index.php'); }//endif post ?> |
At top on index.php page right after body tag
1 2 3 4 5 |
<?php /**********************index.php**/ require_once('global-functions.php'); get_msg(); ?> |
you can improve it more, here for learning purpose I kept it more simpler for beginners to learn easily. try it and comment your reviews, I will try my best to help you more.
your messages will display as below
Comments