Problem Solution Discussion Installing Apache::Session

exam ples are under t he t om cat direct ory. The SQL script s for creat ing t he session st orage t ables are locat ed in t he t ables direct ory. As used here, t he session t ables are creat ed in t he cookbook dat abase and accessed t hrough t he sam e MySQL account as t hat used elsewhere in t his book. I f you dont want t o m ix session m anagem ent act ivit ies wit h t hose pert aining t o t he ot her cookbook t ables, consider set t ing up a separat e dat abase and MySQL account t o be used only for session dat a. This is t rue part icularly for Tom cat , where session m anagem ent t akes place above t he applicat ion level. You m ight not want t he Tom cat server st oring inform at ion in your dat abase; if not , give t he server it s ow n dat abase.

19.2 Using MySQL-Based Sessions in Perl Applications

19.2.1 Problem

You want t o use session st orage for Perl script s.

19.2.2 Solution

The Apache: : Session m odule provides a convenient way t o use several different st orage t ypes, including one based on MySQL.

19.2.3 Discussion

Apache: : Session is an easy- t o-use Perl m odule for m aint aining st at e inform at ion across m ult iple web request s. Despit e t he nam e, t his m odule is not dependent on Apache and can be used in non-web cont ext s, for exam ple, t o m aint ain persist ent st at e across m ult iple invocat ions of a com m and- line script . On t he ot her hand, Apache: : Session doesnt handle any of t he issues associat ed wit h t racking t he session I D sending it t o t he client in response t o t he init ial request and ext ract ing it from subsequent request s . The exam ple applicat ion shown here uses cookies t o pass t he session I D, on t he assum pt ion t hat t he client has cookies enabled.

19.2.4 Installing Apache::Session

I f you dont have Apache: : Session, you can get it from t he CPAN visit ht t p: cpan.perl.org . I nst allat ion is st raight forward, alt hough Apache: : Session does require several ot her m odules t hat you m ay need t o get first . When you inst all it , Apache: : Session should t ell you which required m odules you need if any are m issing. Aft er you have everyt hing inst alled, creat e a t able in which t o st ore session records. The specificat ion for t he t able com es from t he MySQL st orage handler docum ent at ion, which you can read using t his com m and: perldoc Apache::Session::Store::MySQL The t able can be placed in any dat abase you like well use cookbook , but t he t able nam e m ust be nam ed sessions and have t his st ruct ure: CREATE TABLE sessions id CHAR32 NOT NULL, session identifier a_session BLOB, session data PRIMARY KEY id ; The id colum n holds session ident ifiers, which are 32- charact er MD5 values generat ed by Apache: : Session. The a_session colum n holds session dat a in t he form of serialized st rings. Apache: : Session uses t he St orable m odule t o serialize and unserialize session dat a.

19.2.5 The Apache::Session Interface