Session blues with Smarty? [text only]
| From: | René-Marc Simard | Date: | Tue, 31 Jul 2001 01:21:20 +0000 |
| Subject: | Session blues with Smarty? [text only] | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-1300@lists.php.net to get a copy of this message | ||
[Terribly sorry for the previous HTML ridden message, here is the plain text
version]
First of all, cool class!
Here is a problem I faced and my (just found) solution is posted below:
Problem:
I use the 'insert' tag to include a session managing function. The function has
access to session data, but the changes do not 'stick'. The striped down code
goes like:
function insert_update_session_var($tpl_args) {
global $HTTP_SESSION_VARS;
$username = $tpl_args['username'];
$HTTP_SESSION_VARS['username'] = $username;
return $username . ' is some cool fella!';
}
- register_globals is set to off
- using PHP 4.0.5 and Smarty 1.4.4
- returning values or not doesn't affect the issue
- similar script run before Smarty works like a charm
- similar script run after Smarty fails miserably
But the session information unfortunately does not 'stick' and the username is
not registered to my session database using PHP4's 'user' sessions handler. All
I want is to update/enter data into the session! I wish to make more complex
functions to manage session data, but I need these functions to be called by
'insert' tags, so doing session manipulation before displaying Smarty templates
is out of the question.
I've tried using a temporary variable and save the session data after Smarty has
done its job, but storing any kind of session data after displaying Smarty
templates simply fails as well. Even displaying the most simple of templates
("Hello World") prevents me from updating session information afterwards. Only
session manipulation before Smarty works, and that's not of much use. I'm using
the default Smarty configuration.
What could prevent session changes from being tracked properly and saved upon
script completion?
Solution:
I simply added $HTTP_SESSION_VARS to the list of globals for the fetch()
function. Problem solved! Surely I'm not the only one faced with such a need.
Perhaps a similar solution/change could be brought to the next Smarty version?
Anyhow, overall Smarty is a sweet script and obviously the most advanced
template engine I've played with. Congrats to Andrei, Monte, and all the
contributors!
Sincerely,
René-Marc Simard
SciFlicks.com - Science Fiction Cinema