Forums / Developer / Best practice when dealing with session variables?

"Please Note:
  • At the specific request of Ibexa we are changing this projects name to "Exponential" or "Exponential (CMS)" effective as of August, 11th 2025.
  • This project is not associated with the original eZ Publish software or its original developer, eZ Systems or Ibexa".

Best practice when dealing with session variables?

Author Message

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