Embperl - building dynamic websites with Perl


Maintaining persistent (session) data
[ << Prev: Form fields ] [ Content ] [ Next: Breaking your code up into components >> ]
 (Embperl 1.2 or above)

While hidden fields are useful when working with forms, it's often necessary to store persistent data in a more general way. Embperl utilizes Apache::Session to do this job. Apache::Session is caple of storing persistent data in memory, in a textfile or in a database. More storage methods may supported in the future. While you can simply call Apache::Session from an Embperl page, Embperl can do it for you. All you need to do is to put your data in the hash %udat. The next time the same user requests any Embperl page %udat will contain the same data. You can simply use this to keep state information for the user. Depending on your expire settings, the state can also kept between mulitiple sessions. A second hash, %mdat, can be used to keep a state for one page, but for multiple users. A simple example would be a page hit counter:

  The page is requested [+ $mdat{counter}++ +] times
  since [+ $mdat{date} ||= localtime +]

The above example counts the page hits and shows the date when the page is first requested. You don't need to worry about performance - as long as you don't touch %udat or %mdat, no action is taken.


[ << Prev: Form fields ] [ Content ] [ Next: Breaking your code up into components >> ]


© 1997-2023 Gerald Richter / actevy