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
Post a Comment