JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
UNTESTED: added session_messages.php (saves/restores message()s over redirects)
[wfpl.git] / session_messages.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # require_once() this file instead of messages.php if you'd like to store
23 # save/restore messages in the session accross redirect()s. That's all you have
24 # to do. These functions are called when needed (from redirect() and
25 # display_messages()) if they are defined.
26
27 # see messages.php for documentation on how to use it.
28
29 require_once('code/wfpl/session.php');
30 require_once('code/wfpl/string_array.php');
31 require_once('code/wfpl/messages.php');
32
33 function session_save_messages() {
34         if(!isset($GLOBALS['wfpl_messages'])) {
35                 return;
36         }
37         if(!is_array($GLOBALS['wfpl_messages'])) {
38                 return;
39         }
40
41         init_session();
42         session_set('wfpl_messages', array_to_string($GLOBALS['wfpl_messages']);
43 }
44
45 function session_restore_messages() {
46         if(!session()) {
47                 return false;
48         }
49         $messages = session_get('wfpl_messages');
50         if($messages !== false) {
51                 $messages = string_to_array($messages);
52                 # messages from the previous run happened first
53                 $GLOBALS['wfpl_messages'] = array_merge($messages, $GLOBALS['wfpl_messages']);
54         }
55         session_clear('wfpl_messages');
56 }