The Apache::Session Interface

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

To use t he sessions t able in a script , include t he MySQL- relat ed session m odule: use Apache::Session::MySQL; Apache: Session represent s session inform at ion using a hash. I t uses Perls tie m echanism t o m ap hash operat ions ont o t he st orage and ret rieval m et hods used by t he underlying st orage m anager. Thus, t o open a session, you should declare a hash variable and pass it t o tie . The ot her argum ent s t o tie are t he nam e of t he session m odule, t he session I D, and inform at ion about t he dat abase t o use. There are t wo ways t o specify t he dat abase connect ion. First , you can pass a reference t o a hash t hat cont ains connect ion param et ers: my session; tie session, Apache::Session::MySQL, sess_id, { DataSource = DBI:mysql:host=localhost;database=cookbook, UserName = cbuser, Password = cbpass, LockDataSource = DBI:mysql:host=localhost;database=cookbook, LockUserName = cbuser, LockPassword = cbpass }; I n t his case, Apache: : Session uses t he param et ers t o open it s own connect ion t o MySQL, which it closes when you close or dest roy t he session. Second, you can pass t he handle for an already open dat abase connect ion represent ed here by dbh : my session; tie session, Apache::Session::MySQL, sess_id, { Handle = dbh, LockHandle = dbh }; I f you pass a handle t o an open connect ion like t his, Apache: : Session leaves it open when you close or dest roy t he session, on t he assum pt ion t hat youre using t he handle for ot her purposes elsewhere in t he script . You should close t he connect ion yourself when youre done wit h it . The sess_id argum ent t o tie represent s t he session ident ifier. I t s value should be eit her undef t o begin a new session, or a valid I D corresponding t o an exist ing session record. I n t he lat t er case, t he value should m at ch t hat of t he id colum n in som e exist ing sessions t able record. Aft er t he session has been opened, you can access it s cont ent s. For exam ple, aft er opening a new session, youll want t o det erm ine what it s ident ifier is so you can send it t o t he client . That value can be obt ained like t his: sess_id = session{_session_id}; Session hash elem ent nam es t hat begin wit h an underscore such as _session_id ar e reserved by Apache: : Session for int ernal use. Ot her t han t hat , you can use nam es of your own choosing for st oring session values. For exam ple, you m ight m aint ain a scalar count er value as follows, where t he count er is init ialized if t he session is new, t hen increm ent ed and ret rieved for display: session{count} = 0 if exists session{count}; initialize counter ++session{count}; increment counter print counter value: session{count}\n; print value To save a non-scalar value such as an array or a hash int o t he session record, st ore a reference t o it : session{my_array} = \my_array; session{my_hash} = \my_hash; I n t his case, changes m ade t o my_array or my_hash before you close t he session will be reflect ed in t he session cont ent s. To save an independent copy of an array or hash in t he session t hat will not change when you m odify t he original, creat e a reference t o it like t his: session{my_array} = [ my_array ]; session{my_hash} = { my_hash }; To ret rieve a non- scalar value, dereference t he reference st ored in t he session: my_array = {session{my_array}}; my_hash = {session{my_hash}}; To close a session when youre done wit h it , pass it t o untie : untie session; When you close a session, Apache: : Session saves it t o t he sessions t able if youve m ade changes t o it . This also m akes t he session values inaccessible, so dont close t he session unt il youre done accessing it . Apache: : Session not ices changes t o t op- level session record values, but m ight not det ect a change t o a m em ber of a value st ored by reference such as an array elem ent . I f t his is a problem , you can force Apache: : Session t o save a session when you close it by assigning any t op- level session elem ent a value. The session I D is always present in t he session hash, so it provides a convenient way t o force session saving: session{_session_id} = session{_session_id}; An open session m ay be t erm inat ed rat her t han closed. Doing so rem oves t he corresponding record from t he sessions t able, so t hat it can be used no longer: tied session-delete ;

19.2.6 A Sample Application