Forums / Developer / Best practice when dealing with session variables?
Helge Silset
Tuesday 25 January 2011 7:28:11 am
Hi,
We had this line of code throwing errors to our log when the session variable is not set: {def $my_var=ezhttp( 'my_session_var', 'session' )} I've replaced it with these lines to stop the errors: {def $my_var=false()} {if is_set(ezhttp( 'my_session_var', 'session' ))} {set $my_var=ezhttp( 'my_session_var', 'session' )} {/if} I'm not sure if this is the right way to do it though. Is there a better way?Any input appreciated!
Gaetano Giunta
Tuesday 25 January 2011 8:33:30 am
It's verbose but correct
Principal Consultant International Business Member of the Community Project Board
Franck Magnan
Tuesday 25 January 2011 11:48:09 am
Hello Helge, in your second example, error is throwing any way.To avoid it, you can use this syntax:
{def $my_var=false()} {if ezhttp_hasvariable( 'my_session_var', 'session' )} {set $my_var=ezhttp( 'my_session_var', 'session' )} {if} {undef $my_var}
or, if you prefer have it in one line:
{def $my_var=cond(ezhttp_hasvariable( 'my_session_var', 'session' ), ezhttp( 'my_session_var', 'session' ), false())}
-- Developer at Open Wide