O'Reilly logo

Writing Apache Modules with Perl and C by Lincoln Stein, Doug MacEachern

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

F.3. Storing Persistent Data

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 capable of storing persistent data in memory, in a text file, or in a database. More storage methods may be supported in the future. Although you can simply call Apache::Session from an Embperl page, Embperl can call it for you. All you need to do is to put user data in the hash %udat. The next time the same user requests any Embperl page, %udat will contain the same data. You can use this approach to keep state information for the user, and depending on your expire settings, you can also keep state between multiple sessions. A second hash, %mdat, can be used to keep state for one page for multiple users. A simple example would be a page hit counter:

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

This example counts the page hits and shows the date when the page is first requested. (See the hangman game at the end of this appendix for more examples of %udat and %mdat.) You don't need to worry about performance—as long as you don't touch %udat or %mdat, no action is taken.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required