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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 04:57:44
Script start
Timing: Jan 18 2025 04:57:44
Module start 'layout'
Timing: Jan 18 2025 04:57:44
Module start 'content'
Timing: Jan 18 2025 04:57:45
Module end 'content'
Timing: Jan 18 2025 04:57:45
Script end

Main resources:

Total runtime1.4235 sec
Peak memory usage4,096.0000 KB
Database Queries59

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0091 589.1641152.6406
Module start 'layout' 0.00910.0030 741.804739.5000
Module start 'content' 0.01211.4103 781.3047577.4922
Module end 'content' 1.42240.0011 1,358.796912.1250
Script end 1.4235  1,370.9219 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.2329160.0002
Check MTime0.00130.0909160.0001
Mysql Total
Database connection0.00170.117710.0017
Mysqli_queries1.365195.8961590.0231
Looping result0.00070.0466570.0000
Template Total1.379796.920.6898
Template load0.00210.150820.0011
Template processing1.377596.767720.6888
Template load and register function0.00010.010510.0001
states
state_id_array0.00230.163510.0023
state_identifier_array0.00100.072120.0005
Override
Cache load0.00180.1239240.0001
Sytem overhead
Fetch class attribute can translate value0.00050.034130.0002
Fetch class attribute name0.00100.071450.0002
XML
Image XML parsing0.00200.140230.0007
class_abstraction
Instantiating content class attribute0.00000.000650.0000
General
dbfile0.00730.5135210.0003
String conversion0.00000.000640.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
5content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 16
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs