Using PHP sessions to send messages to the user -


i have session class follows:

<?php  class session {      function __construct() {         if (empty(session_id())) {             session_start();         }     }      function addmessage($msg="") {         if (!is_array($_session['messages'])) {             $_session['messages'] = [];         }         array_push($_session['messages'], $msg);     }      function getmessages() {         $messages = $_session['messages'];         unset($_session['messages']);         return $messages;     }  }  ?> 

also there php file in layouts directory:

<?php if (!empty($_session['messages'])) { ?> <div class="messages"> <ul> <?php     $messages = $session->getmessages();     foreach ($messages $message) {         echo "<li class=\"message\">{$message}</li>";     } ?> </ul> </div> <?php }?> 

this piece of code above included @ top of pages. problem in case of single-page submission handling -done in middle of page - messages printed out before set addmessage() method.

is there simple way around issue, or have rethink code flow?


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -